mirror of
https://github.com/doctrine/orm.git
synced 2026-03-24 06:52:09 +01:00
Use composite foreign key a part of a composite primary key #5474
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 @TheBay0r on GitHub (Mar 22, 2017).
Originally assigned to: @Ocramius on GitHub.
I try to create a composite primary key where one part of it, is a composite foreign key. The code looks like following:
Sadly this scenario doesn't seem to work. Am I doing something wrong or is it simply not supported?
Without having a composite foreign key I at least get the composite primary.. But what I would want to have in the DB is something like:
@Ocramius commented on GitHub (Mar 22, 2017):
What does it do?
@TheBay0r commented on GitHub (Mar 22, 2017):
Oh sry, forgot to add that:
@Ocramius commented on GitHub (Mar 22, 2017):
That seems really buggy - shouldn't just drop columns like that...
Does the ORM schema validator work?
@TheBay0r commented on GitHub (Mar 23, 2017):
Everything seems fine according to the validator:
@TheBay0r commented on GitHub (Mar 24, 2017):
Hm, looks like there was a special character in front of
@ORM\Id, so doctrine wouldn't recognise it, atNow that I have re-written that line I get:
This actually answers the question i guess 😞
@Ocramius commented on GitHub (Mar 24, 2017):
@TheBay0r yeah, it's currently a limitation, and it was never cleaned up because it's a huge amount of work for very little gain. It's much simpler to have a synthetic PK and unique indexes for what your model understands as PK.
@yurii-github commented on GitHub (Sep 24, 2017):
the limitation is real.. instead of 2 tables i'm forced to use 4 tables
@Ocramius commented on GitHub (Sep 24, 2017):
@yurii-github if you want, you can analyse the code locations affected by this. The main problem is generating unique and non-colliding flat identifier fields for hashing within the UnitOfWork and all layers of the ORM
@garycla commented on GitHub (Oct 18, 2017):
I can understand this is a huge amount of work (I thank you for your work on this very good tool) and it is true we can turn this limitation around but when you say it is very little gain... I disagree. Having composite keys is rather common (and doctrine is said to support composite keys natively : "Doctrine 2 supports composite primary keys natively. Composite keys are a very powerful relational database concept"). What you say is just that we can have composite PK but not many to many relationships between 2 entities like this.
In my experience, this is very common and models become much more complex and less readable to handle this (much more FK or joins)
@Ocramius commented on GitHub (Oct 18, 2017):
These are supported. What isn't supported is using a composite identity of an associated entity as part of the identity of an entity.
You are more than welcome to start implementing a solution for this problem-space, then (you can start from just a test).
Doctrine ORM is already quite ahead on anything regarding composite PKs, but it is not a trivial problem to solve within PHP's performance constraints.
@garycla commented on GitHub (Oct 18, 2017):
I'm not sure I have understood (i'm french). Anyway, i think what you say is what i meant to be common : when I have an entity with a FK to another one, being part of its PK, this PK can't take part in a many to many relationship, which is a situation i meet quite often
It seems to be a high hill for me (Doctrine is quite a big project) but I can try to help...
@Ocramius commented on GitHub (Oct 18, 2017):
The entity generator is a rudimentary tool only meant to get you started.
If you look at https://github.com/doctrine/doctrine2/tree/master/tests/Doctrine/Tests/ORM/Functional/Ticket you can already find examples of existing integration tests.
@garycla commented on GitHub (Oct 18, 2017):
sorry for the update of my comment while you were answering. I re read it and saw i didn't answered right.
@garycla commented on GitHub (Oct 18, 2017):
As I am in an offensive day :-) I'd say this is some kind of pity as i like to create my database from modelling tools (when you have more than 100 tables, it becomes hard to keep the whole schema in mind without graphical tool). So it would be very practical to generate entities from DB...
Well. Anyway, all this is to tickle. Doctrine is a very good tool and I will be able to manage these little flaws
@Ocramius commented on GitHub (Oct 18, 2017):
It has been abused to the point that we're probably dropping the generator from ORM core at some point. Don't expect it to become better: entities should be designed first, and the database is a detail that comes later. If you go the opposite way, there's a good chance that the ORM is more of a problem than an useful tool for you.
@garycla commented on GitHub (Oct 18, 2017):
I can understand this.
But it is very "painful" to write php "stupid" classes for 100's of objects after having modelled them in a graphical tool.
In this case, maybe would it be interesting to provide a generator from a UML modeller.