Doctrine ignores Index and just takes UniqueConstraint into account for the same field #5894

Closed
opened 2026-01-22 15:21:25 +01:00 by admin · 2 comments
Owner

Originally created by @bentcoder on GitHub (Feb 20, 2018).

Originally assigned to: @Ocramius on GitHub.

Hi,

I am not sure if this is a bug or an expected behaviour of Doctrine.

If I set UniqueConstraint and Index on a same field, Doctrine ignores creating a Index entry in database. However, if I remove UniqueConstraint, Doctrine creates Index entry in database. Seems like we cannot set both UniqueConstraint and Index on a same field with Doctrine. Doctrine prefers UniqueConstraint over Index if they both set on a same field.

Note: At database level, we can manually set UNQ and IDX on same field.

Thanks

MAPPING 1

/**
 * @ORM\Entity
 * @ORM\Table(
 *     name="country",
 *     uniqueConstraints={
 *          @ORM\UniqueConstraint(name="UNQ_name", columns={"name"}),
 *          @ORM\UniqueConstraint(name="UNQ_abbreviation", columns={"abbreviation"})
 *     },
 *     indexes={
 *          @ORM\Index(name="IDX_abbreviation", columns={"abbreviation"}),
 *     }
 * )
 */

DQL 1

  UNIQUE KEY `UNQ_name` (`name`)
  UNIQUE KEY `UNQ_abbreviation` (`abbreviation`)

MAPPING 2

/**
 * @ORM\Entity
 * @ORM\Table(
 *     name="country",
 *     uniqueConstraints={
 *          @ORM\UniqueConstraint(name="UNQ_name", columns={"name"})
 *     },
 *     indexes={
 *          @ORM\Index(name="IDX_abbreviation", columns={"abbreviation"}),
 *     }
 * )
 */

DQL 2

  UNIQUE KEY `UNQ_name` (`name`)
  KEY `IDX_abbreviation` (`abbreviation`)
Originally created by @bentcoder on GitHub (Feb 20, 2018). Originally assigned to: @Ocramius on GitHub. Hi, I am not sure if this is a bug or an expected behaviour of Doctrine. If I set `UniqueConstraint` and `Index` on a same field, Doctrine ignores creating a `Index` entry in database. However, if I remove `UniqueConstraint`, Doctrine creates `Index` entry in database. Seems like we cannot set both `UniqueConstraint` and `Index` on a same field with Doctrine. Doctrine prefers `UniqueConstraint` over `Index` if they both set on a same field. **Note**: At database level, we can manually set UNQ and IDX on same field. Thanks **MAPPING 1** ``` /** * @ORM\Entity * @ORM\Table( * name="country", * uniqueConstraints={ * @ORM\UniqueConstraint(name="UNQ_name", columns={"name"}), * @ORM\UniqueConstraint(name="UNQ_abbreviation", columns={"abbreviation"}) * }, * indexes={ * @ORM\Index(name="IDX_abbreviation", columns={"abbreviation"}), * } * ) */ ``` **DQL 1** ``` UNIQUE KEY `UNQ_name` (`name`) UNIQUE KEY `UNQ_abbreviation` (`abbreviation`) ``` -------- **MAPPING 2** ``` /** * @ORM\Entity * @ORM\Table( * name="country", * uniqueConstraints={ * @ORM\UniqueConstraint(name="UNQ_name", columns={"name"}) * }, * indexes={ * @ORM\Index(name="IDX_abbreviation", columns={"abbreviation"}), * } * ) */ ``` **DQL 2** ``` UNIQUE KEY `UNQ_name` (`name`) KEY `IDX_abbreviation` (`abbreviation`) ```
admin added the InvalidQuestion labels 2026-01-22 15:21:25 +01:00
admin closed this issue 2026-01-22 15:21:25 +01:00
Author
Owner

@Ocramius commented on GitHub (Feb 20, 2018):

This is expected behavior.

Ref: https://github.com/doctrine/dbal/pull/769
Ref: https://github.com/doctrine/dbal/pull/764

Can't find the original issue where we actually decided to de-duplicate overlapping/redundant indexes.

@Ocramius commented on GitHub (Feb 20, 2018): This is expected behavior. Ref: https://github.com/doctrine/dbal/pull/769 Ref: https://github.com/doctrine/dbal/pull/764 Can't find the original issue where we actually decided to de-duplicate overlapping/redundant indexes.
Author
Owner

@bentcoder commented on GitHub (Feb 20, 2018):

I've went through those links but there is nothing clear there as there are too many references all over the place.

As far as I can see this is how Doctrine intend to work.

@bentcoder commented on GitHub (Feb 20, 2018): I've went through those links but there is nothing clear there as there are too many references all over the place. As far as I can see this is how Doctrine intend to work.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5894