I have the following Doctrine Entity in Symfony 4.2:
When I run the migration the unique constraints and the indexes are not generated. The result of the Entity are just the following indexes in MySql:
<?php
// src/Entity/Media.php
namespace App\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\ORM\Mapping\UniqueConstraint;
/**
* Common\Model\Entity\Media
*
* @Entity
* @Table(name="media",
* uniqueConstraints={
* @UniqueConstraint(name="virtual_unique",
* columns={"virtualname", "virtualfolder"})
* }
* indexes={
* @Index(name="idx_virtual", columns={"virtualname", "virtualfolder"})
* @Index(name="idx_identifier", columns={"identifier"})
* }
* )
*/
/**
* @ORM\Entity(repositoryClass="App\Repository\MediaRepository")
*/
class Media
{
/**
* @ORM\Id
* @ORM\GeneratedValue
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\Column(type="string", nullable=true, options={"unique": true})
*/
private $identifier;
/**
* @ORM\Column(type="string", length=255, nullable=false)
*/
private $virtualname;
/**
* @ORM\Column(type="string", length=255, nullable=true)
*/
private $virtualfolder;
}
Probably you need change @Table
to @ORM\\Table
and remove @Entity
try this:
/**
* @ORM\Entity(repositoryClass="App\Repository\MediaRepository")
* @ORM\Table(name="media",
* uniqueConstraints={
* @UniqueConstraint(name="virtual_unique",
* columns={"virtualname", "virtualfolder"})
* }
* indexes={
* @ORM\Index(name="idx_virtual", columns={"virtualname", "virtualfolder"})
* @ORM\Index(name="idx_identifier", columns={"identifier"})
* }
* )
*/
First, you have multiple annotation blocks for the same class. Just merge them and have a single annotation block for that entity.
And you are using serveral annotations directly, but not importing their definitions . Specifically:
@Entity
@Index
@UniqueConstraint
@Table
Either you:
@ORM
(since you are doing use Doctrine\\ORM\\Mapping as ORM
)This is the most convenient and the one that's usually applied.
A complete fix:
/**
* Common\Model\Entity\Media
*
* @ORM\Entity(repositoryClass="App\Repository\MediaRepository")
* @ORM\Table(name="media",
* uniqueConstraints={
* @ORM\UniqueConstraint(name="virtual_unique",
* columns={"virtualname", "virtualfolder"})
* },
* indexes={
* @ORM\Index(name="idx_virtual", columns={"virtualname", "virtualfolder"}),
* @ORM\Index(name="idx_identifier", columns={"identifier"})
* }
* )
*/
class Media
{}
You could also use the annotations with the full namespace, like @Doctrine\\ORM\\Mapping\\Table
, @Doctrine\\ORM\\Mapping\\UniqueConstraint
, etc; but that gets unnecessarily verbose very quickly.
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\Index;
use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\UniqueConstraint;
But it also seems wasteful.
The approach you'll see on the docs is the first one. Import Doctrine\\ORM\\Mapping
as ORM
, and use the annotations hanging from there.
I tried a few other suggestions, but after a cache:clear
I got the index in my migration.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.