mirror of
https://github.com/doctrine/orm.git
synced 2026-03-23 22:42:18 +01:00
Unique Constraints etc. defined on STI entities are ignored #7213
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Originally created by @simPod on GitHub (Aug 10, 2023).
Bug Report
Summary
This condition aborts processing of STI entities when creating a Schema
597a63a86c/lib/Doctrine/ORM/Tools/SchemaTool.php (L140)(it would require more changes in order to make it work)
Current behavior
Unique constraints are ignored, (
[ 'a', 'b' ]in example bellow)How to reproduce
Expected behavior
Not ignored.
[ 'a', 'b' ]UC is created.@derrabus commented on GitHub (Aug 10, 2023):
Why don't you declare the
UniqueConstrainton classA?@simPod commented on GitHub (Aug 10, 2023):
Because the UniqueConstraint is related to
B, notA.Bhas fields contained in unique constraint that are not present onA.@derrabus commented on GitHub (Aug 10, 2023):
I see. But would it work?
I mean, all child entities of
Awill be stored in A's table. That means any constraint applies to all of A's children. You cannot have a constraint that only applies toBinstances.@simPod commented on GitHub (Aug 10, 2023):
Yup, it will.
Because
Bhas unique fields that other children do not have. So having a UC with those fields on common table will not affect other children as they will havenullvalue in those fields.E.g.
Bhas fieldfooAnor other children has no such fieldfoofoofield in DBnullvalues infoofieldIn code term I want this
But currently I have to do this
which works ->
so yes.
@derrabus commented on GitHub (Aug 10, 2023):
Perfect. 🙂
And that looks perfectly fine to me. With single-table inheritance, the root entity controls the table and the children can only add nullable fields to it. Those settings have to go somewhere and unless we have a very good reason to, I would not add the complexity of scraping and merging table settings from all children.
What we can do here:
Table,UniqueConstraintand friends are used on children in a single-table inheritance.@simPod commented on GitHub (Aug 10, 2023):
Hmm, since ORM should abstract me from looking at things from db perspective as much as possible, I did not want to think about the parent class as a table controller for all subclasses, but rather have relevant constraints etc. coupled with related subclasses. I can live with that of course.
However,
UniqueConstraintwithfieldson parent class is ignored when it contains fields that are not present on the parent class but child. That goes a bit againstYou have to use
UniqueConstraintwithcolumnsin order for it to work now.@derrabus commented on GitHub (Aug 13, 2023):
Well yes, but the metadata mapping is the way you tell the ORM how it should do that. And annotations like
UniqueConstraintsimply don't make sense in a context where the database has been abstracted away completely.Let's fix that?
@Pixelshaped commented on GitHub (Oct 25, 2023):
I can confirm that when
UniqueConstraintis declared on a parent class (seemingly the one that also declares the doctrine inheritance), thenfieldsdoes not work, onlycolumns.