Orm: Simple annotation for single-column index

Created on 22 Jan 2017  路  9Comments  路  Source: doctrine/orm

I'd welcome a simple way to add index to a single column, some shorthand for this:

/**
 * @ORM\Entity
 * @ORM\Table(indexes={@Index(name="foo_idx", columns={"foo"})})
 */
class Foo
{
    /** @ORM\Column(type="string") */
    private $foo;
}

to be annotated directly with the column:

/** @ORM\Entity */
class Foo {
    /** @ORM\Column(type="string", index=true) */
    private $foo;
}

or in a similar way (like @ORM\Index for column). This is possible for unique indexes as Column(unique=true), so I think there should be similar annotation for non-unique indexes.

Any comments welcomed, I can eventually pull-request it if it makes sense for others too.

Improvement Question

Most helpful comment

With that simple annotation, we could put indexes on single-columns added from Traits.
Now, i have no idea on how i can manage index from a trait.

All 9 comments

Seems like an unnecessary shortcut to me, much like unique (which can also be defined via @UniqueConstraint.

Possibly simplifying things on one side, but adding more paths where to look for index definitions.

Actually, this calls for dropping unique from the @Column annotation. Thoughts, @guilhermeblanco?

Well that's not exactly what I wanted but still better than current inconsistency in key definition. So, thumbs up for removing.

Just note that dropping unique from @Column will require all unique constraints to be declared in parent entity, no matter where the field is declared, as per #6248.

@Ocramius any idea on how this relates with the JPA 2.2 stuff?

@lcobucci no, sorry.

From JPA 2.2 spec:
Column annotation spec

There is no indexed or similar attribute.

So I wouldn't remove the unique property...

With that simple annotation, we could put indexes on single-columns added from Traits.
Now, i have no idea on how i can manage index from a trait.

Was this page helpful?
0 / 5 - 0 ratings