DDC-1114: Association on a non primary key field - fix the commit order #1395

Closed
opened 2026-01-22 13:13:01 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Apr 14, 2011).

Originally assigned to: @guilhermeblanco on GitHub.

Jira issue originally created by user dan_liip:

We are trying to create an association which referenced column is not a primary key.

The source entity mapping looks like:

The destination entity mapping is:

<entity name="Joiz\ShowBundle\Entity\ShowInstance" table="show_instance" repository-class="Joiz\ShowBundle\Entity\ShowInstanceRepository">

    <id name="id" column="id" type="integer">
        <generator strategy="IDENTITY" />
    </id>

    <field name="extid" column="extid" type="string" unique="true" />

    <!-- ... snip ... -->

</entity>

Doctrine will correctly create the tables with the above mapping but the association does not seem to work.

When we try to load some data in the tables:

    $showInst = new ShowInstance();
    $showInst->setExtid(999);
    $manager->persist($showInst);

    $teaser = new ShowInstanceTeaser();
    $teaser->setShowInstance($showInst);
    $manager->persist($teaser);

This will leave the column "teaser.show_instance_extid" null.

Doing the same but using the primary key for the association will work.

In a discussion on the #doctrine-dev channel on freenode beberlei explained:

ah the problem is probably the CommitOrderCalculator does not know of this foreign key
and sorts the inserts in the wrong order

you cannot add foreign keys say through some mapping information in annotations or xml the like
you have to create them manually
and in this case doctrine might end up in troubles depending on the commit order
hibernate solves this by allowing to specifiy additional constraints that are recognized while calculating the commit order, but this is not yet in tdoctrine2. can you open up a ticket for this?

Originally created by @doctrinebot on GitHub (Apr 14, 2011). Originally assigned to: @guilhermeblanco on GitHub. Jira issue originally created by user dan_liip: We are trying to create an association which referenced column is not a primary key. The source entity mapping looks like: <entity name="Joiz\HomeBundle\Entity\ShowInstanceTeaser" table="teaser"> <many-to-one field="showInstance" target-entity="Joiz\ShowBundle\Entity\ShowInstance"> <join-column name="show*instance*extid" referenced-column-name="extid" /> </many-to-one> </entity> The destination entity mapping is: ``` <entity name="Joiz\ShowBundle\Entity\ShowInstance" table="show_instance" repository-class="Joiz\ShowBundle\Entity\ShowInstanceRepository"> <id name="id" column="id" type="integer"> <generator strategy="IDENTITY" /> </id> <field name="extid" column="extid" type="string" unique="true" /> <!-- ... snip ... --> </entity> ``` Doctrine will correctly create the tables with the above mapping but the association does not seem to work. When we try to load some data in the tables: ``` $showInst = new ShowInstance(); $showInst->setExtid(999); $manager->persist($showInst); $teaser = new ShowInstanceTeaser(); $teaser->setShowInstance($showInst); $manager->persist($teaser); ``` This will leave the column "teaser.show_instance_extid" null. Doing the same but using the primary key for the association will work. In a discussion on the #doctrine-dev channel on freenode beberlei explained: <beberlei> ah the problem is probably the CommitOrderCalculator does not know of this foreign key <beberlei> and sorts the inserts in the wrong order <beberlei> you cannot add foreign keys say through some mapping information in annotations or xml the like <beberlei> you have to create them manually <beberlei> and in this case doctrine might end up in troubles depending on the commit order <beberlei> hibernate solves this by allowing to specifiy additional constraints that are recognized while calculating the commit order, but this is not yet in tdoctrine2. can you open up a ticket for this?
admin added the New Feature label 2026-01-22 13:13:01 +01:00
admin closed this issue 2026-01-22 13:13:02 +01:00
Author
Owner

@doctrinebot commented on GitHub (Apr 14, 2011):

@doctrinebot commented on GitHub (Apr 14, 2011): - relates to [DDC-1269: Unexpected behavior while using association on a non primary key field](http://www.doctrine-project.org/jira/browse/DDC-1269)
Author
Owner

@doctrinebot commented on GitHub (Jul 12, 2011):

Comment created by @beberlei:

Btw, I don't think its supported to use a non primary id for foreign key matching. I cant tell for sure though since i wasn't responsible to design this part of the Doctrine code. I would strongly suggest not to do this.

@doctrinebot commented on GitHub (Jul 12, 2011): Comment created by @beberlei: Btw, I don't think its supported to use a non primary id for foreign key matching. I cant tell for sure though since i wasn't responsible to design this part of the Doctrine code. I would strongly suggest not to do this.
Author
Owner

@doctrinebot commented on GitHub (Sep 5, 2011):

Comment created by @guilhermeblanco:

Any ORM can't be designed to map associations on non-ID fields.

Ideally, no IDs would be exposed to user (they should be controlled internally), but since almost everything rely on IDs to be passed this is exposed to the user.

Anyway, this ticket is invalid as it's an invalid usage of the ORM tool.

@doctrinebot commented on GitHub (Sep 5, 2011): Comment created by @guilhermeblanco: Any ORM can't be designed to map associations on non-ID fields. Ideally, no IDs would be exposed to user (they should be controlled internally), but since almost everything rely on IDs to be passed this is exposed to the user. Anyway, this ticket is invalid as it's an invalid usage of the ORM tool.
Author
Owner

@doctrinebot commented on GitHub (Sep 5, 2011):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (Sep 5, 2011): Issue was closed with resolution "Invalid"
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1395