When child enitity inherits from parent entity and declares some additional @UniqueConstraints using @Table annotation's uniqueConstraints property, SchemaTool silently ignores those annotations.
An (simplified) example:
<?php
/**
* @ORM\Entity()
* @ORM\Table(name="phone_book")
* @ORM\InheritanceType("SINGLE_TABLE")
* @ORM\DiscriminatorColumn("entry_type")
* @ORM\DiscriminatorMap({
* "person" = "PersonPhoneBookEntry",
* "organization" = "OrganizationPhoneBookEntry", // class not pictured in this example
* })
*/
abstract class PhoneBookEntry {
/**
* @var int
* @ORM\Column(type="integer")
*/
private $phoneBookId;
/**
* @var string
* @ORM\Column(type="string")
*/
private $phoneNumber;
}
/**
* @ORM\Entity()
* @ORM\Table(uniqueConstraints={
* @ORM\UniqueConstraint(
* name="phone_book_id__person_id",
* columns={"phone_book_id", "person_id"}
* ),
* })
*/
class PersonPhoneBookEntry extends PhoneBookEntry
{
/**
* @var integer
* @ORM\Column(type="integer")
*/
private $personId;
}
I expect the unique constraints to be created, but documentation does not explictly mention behaviour, when entity is inherited:
Annotation is used inside the
@Tableannotation on the entity-class level. It allows to hint the SchemaTool to generate a database unique constraint on the specified table columns. It only has meaning in the SchemaTool schema generation context.
Doctrine version:
doctrine/common v2.6.1
doctrine/dbal v2.5.4
doctrine/orm v2.5.4
The child class re-defines @Table here: seems expected to me. Also,
please note that annotations are not inherited.
On 20 Jan 2017 6:20 p.m., "Christopher Chrapka" notifications@github.com
wrote:
When child enitity inherits from parent entity and declares some
additional @UniqueConstraints using @Table annotation's uniqueConstraints
property, SchemaTool silently ignores those annotations.An (simplified) example:
* * @ORM\Entity() * @ORM\Table(name="phone_book") * @ORM\InheritanceType("SINGLE_TABLE") * @ORM\DiscriminatorColumn("entry_type") * @ORM\DiscriminatorMap({ * "person" = "PersonPhoneBookEntry", * "organization" = "OrganizationPhoneBookEntry", // class not pictured in this example * }) */abstract class PhoneBookEntry { /* * @var int * @ORM\Column(type="integer") / private $phoneBookId; /* * @var string * @ORM\Column(type="string") / private $phoneNumber;}/ @ORM\Entity()* @ORM\Table(uniqueConstraints={* @ORM\UniqueConstraint(* name="phone_book_id__person_id",* columns={"phone_book_id", "person_id"}* ),* })*/class PersonPhoneBookEntry extends PhoneBookEntry{ / * @var integer * @ORM\Column(type="integer") */ private $personId;}
I expect the unique constraints to be created, but documentation does not
explictly mention behaviour, when entity is inherited:Annotation is used inside the @Table annotation on the entity-class
level. It allows to hint the SchemaTool to generate a database unique
constraint on the specified table columns. It only has meaning in the
SchemaTool schema generation context.Doctrine version:
doctrine/common v2.6.1
doctrine/dbal v2.5.4
doctrine/orm v2.5.4—
You are receiving this because you are subscribed to this thread.
Reply to this email directly, view it on GitHub
https://github.com/doctrine/doctrine2/issues/6248, or mute the thread
https://github.com/notifications/unsubscribe-auth/AAJakDpBNeO0Ot0sYcEed9qD-Xp_wR7Pks5rUOz5gaJpZM4Lpizx
.
If the annotations are not inherited, this is not so much as a re-definition, but additional declaration not directly linked to the one on parent class, in my opinion. Otherwise, I think it should be noted in the documentation, that @Table annotation makes sense only on parent class.
Anyhow, if this is not a desired behaviour, how can I declare the constraints on fields mapped to inherited class without annotation in parent class knowing about them?
how can I declare the constraints on fields mapped to inherited class without annotation in parent class knowing about them?
In a single table inheritance, the root table will have those constraints defined on columns of the root table only. The root table shouldn't concern itself with child table/entity attributes.
The root table shouldn't concern itself with child table/entity attributes.
Yes, but I want to have constraints on combination of fields mapped in root entity and child entity. Should the annotations on root table know about fields from child entity? In our case it even happens, that child entity is declared in another bundle which might be or might not be loaded in given installation of application. If the constraints are declared on parent entity, schema tool will fail when the other bundle is not loaded.
@OJezu did you find a solution for this?
@hgraca be careful when generating Doctrine Migration classes, so they don't remove those constraints
I have the same problem. Any updates in 2018? :)
https://github.com/doctrine/doctrine2/issues/6248#issuecomment-274653210
In a single table inheritance, the root table will have those constraints defined on columns of the root table only. The root table shouldn't concern itself with child table/entity attributes.
In some cases, such constraints are required for a child Entity.
https://github.com/doctrine/doctrine2/issues/6248#issuecomment-274131653
The child class re-defines
@tablehere...
When using XML-based mapping files, the same thing occurs when putting a <unique-constraints/> element in a child Entity: it is ignored. This can be "resolved" or better: circumvented by adding the <unique-constraints/> element to the metadata of the root Entity. Which should actually not even be aware of such information.
Hello @OJezu any updates for this ?
you need just to add the constraint in super class table
@ORM\Table(name="metric", indexes={@ORM\Index(name="idx_metric_created_by", columns={"created_by"})},
* uniqueConstraints={
* @ORM\UniqueConstraint(
* name="test_name",
* columns={"test_id", "name"}
* ),
* })
and it will check for the child entity. t worked fine for me.
@mayro doesn't work for me. What version of Doctrine are you referring to? I added my unique constraints at the super class for STI, which includes those columns that need to be unique from the children entities.
@OJezu any updates?
I have the same situation. I'm using xml mapping. Doctrine ignores my indexes declared in child entity as well as unique constraints.
Any updates?
Same issue... all entities inherit from base classes and are intended to allow for redefining the table name if needed, but would like for them not to have to redefine unique indexes inherent to the parent data model.
Most helpful comment
Yes, but I want to have constraints on combination of fields mapped in root entity and child entity. Should the annotations on root table know about fields from child entity? In our case it even happens, that child entity is declared in another bundle which might be or might not be loaded in given installation of application. If the constraints are declared on parent entity, schema tool will fail when the other bundle is not loaded.