php 8 #[UniqueConstraint] silently ignored without #[Table] attribute #6710

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

Originally created by @Nemo64 on GitHub (May 8, 2021).

I'm playing around with the doctrine 2.9 branch and attributes and notice that the syntax for indexes changed.
https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/attributes-reference.html#uniqueconstraint

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\UniqueConstraint;

#[Entity]
#[UniqueConstraint(name: "ean", columns=["ean"])]
class ECommerceProduct
{
}

I really like it, the nested annotations always looked ugly to me.
However, this example does not work. You must also add a #[Table] attribute, so this:

use Doctrine\ORM\Mapping\Entity;
use Doctrine\ORM\Mapping\Table;
use Doctrine\ORM\Mapping\UniqueConstraint;

#[Entity]
#[Table]
#[UniqueConstraint(name: "ean", columns=["ean"])]
class ECommerceProduct
{
}

I guess that this makes sense, since the constraint mapping belongs to the table mapping, but the current behavior of silently ignoring isn't great. It should:

  • either mark the schema as invalid
  • or implicitly assume that you defined the table with all default definitions (which i'd prefer)
Originally created by @Nemo64 on GitHub (May 8, 2021). I'm playing around with the doctrine 2.9 branch and attributes and notice that the syntax for indexes changed. https://www.doctrine-project.org/projects/doctrine-orm/en/2.9/reference/attributes-reference.html#uniqueconstraint ```php use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\UniqueConstraint; #[Entity] #[UniqueConstraint(name: "ean", columns=["ean"])] class ECommerceProduct { } ``` I really like it, the nested annotations always looked ugly to me. However, this example does not work. You must also add a `#[Table]` attribute, so this: ```php use Doctrine\ORM\Mapping\Entity; use Doctrine\ORM\Mapping\Table; use Doctrine\ORM\Mapping\UniqueConstraint; #[Entity] #[Table] #[UniqueConstraint(name: "ean", columns=["ean"])] class ECommerceProduct { } ``` I guess that this makes sense, since the constraint mapping belongs to the table mapping, but the current behavior of silently ignoring isn't great. It should: - either mark the schema as invalid - or implicitly assume that you defined the table with all default definitions (which i'd prefer)
admin added the Bug label 2026-01-22 15:37:21 +01:00
admin closed this issue 2026-01-22 15:37:22 +01:00
Author
Owner

@beberlei commented on GitHub (May 8, 2021):

Good catch! This is a bug

@beberlei commented on GitHub (May 8, 2021): Good catch! This is a bug
Author
Owner

@beberlei commented on GitHub (May 9, 2021):

Fixed in #8677

@beberlei commented on GitHub (May 9, 2021): Fixed in #8677
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#6710