我有一个实体UserWatchlist
,与User
有关(多对一)。它在2个字段user
和direction
上具有复杂的唯一索引:
use Symfony\Bridge\Doctrine\Validator\Constraints\UniqueEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserWatchlistRepository")
* @ORM\Table(uniqueConstraints={@ORM\UniqueConstraint(columns={"user_id", "direction_id"})})
* @UniqueEntity(
* fields={"user", "direction"},
* errorPath="direction",
* message="User already has this direction"
* )
*/
class UserWatchlist
{
而User::$id
PK是整数
Class User
/**
* @ORM\Id()
* @ORM\GeneratedValue()
* @ORM\Column(type="integer")
*/
private $id;
唯一性检查正常。
但是自从我将其更新为GUID类型:
Class User
/**
* @ORM\Id()
* @ORM\GeneratedValue(strategy="UUID")
* @ORM\Column(type="guid")
*/
private $id;
此检查被忽略,而仅进行数据库级索引检查。
所有索引和外键都可以。
这是错误还是我做错了什么主意?
修改注释后,您需要在数据库中创建迁移并更改架构:
php bin/console make:migration
php bin/console doctrine:migrations:migrate
阅读Symfony docs about Doctrine and database中的更多内容。
FYI有一个工具可以检查您的映射注释是否与数据库同步:
php bin/console doctrine:schema:validate