Why doctrine migrations ignore my index declaration?

880 Views Asked by At

I would like to create a table with an indexed columned to speed up searches.

Here is a sample:

#[ORM\Entity(repositoryClass: SettingRepository::class)]
#[ORM\Table(name: '`tr_setting`', indexes: [
    new ORM\Index(columns: ['code'], name: 'idx_setting_code')
])]
class Setting
{
    #[ORM\Column(type: 'string', length: 15)]
    private ?string $code;

    #[ORM\Column(type: 'text')]
    private string $content;

    #[ORM\Id]
    #[ORM\GeneratedValue]
    #[ORM\Column(type: 'integer')]
    private ?int $id;

    // Getter and setter...
}

When I use DoctrineBundle migration, the file is generated, but index is ignored...

// ....
final class Version20220719140604 extends AbstractMigration
{
    public function up(Schema $schema): void
    {
        // this up() migration is auto-generated, please modify it to your needs
        $this->addSql('CREATE SEQUENCE "tr_setting_id_seq" INCREMENT BY 1 MINVALUE 1 START 1');
        $this->addSql('CREATE TABLE "tr_setting" (id INT NOT NULL, code VARCHAR(15) NOT NULL, content TEXT NOT NULL, PRIMARY KEY(id))');
    }

For other projects, I already did it with annotations instead of attributes.

I carefully read this answer, but it doesn't help me.

I'm on PHP8.1.8, doctrine/orm 2.12.3 , doctrine-migrations: 3.2, postgresql: 13

1

There are 1 best solutions below

0
Alexandre Tranchant On BEST ANSWER

My index shall be declared with this syntax:

#[
    ORM\Entity(repositoryClass: SettingRepository::class),
    ORM\Table(name: '`tr_setting`'),
    ORM\Index(columns: ['code'], name: 'idx_setting_code'),
]

or this one:

#[ORM\Entity(repositoryClass: SettingRepository::class)]
#[ORM\Table(name: '`tr_setting`')]
#[ORM\Index(columns: ['code'], name: 'idx_setting_code')]