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

Open
opened 2026-01-22 15:21:22 +01:00 by admin · 0 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:22 +01:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#5892