DDC-891: DDC-117: No sequence generation with composite foreign key #1105

Open
opened 2026-01-22 13:02:25 +01:00 by admin · 4 comments
Owner

Originally created by @doctrinebot on GitHub (Nov 25, 2010).

Jira issue originally created by user felicitus:

Given the following entity definitions, Doctrine does not attempt to manage generated values. For example, in MySQL, it is perfectly possible to create a composite primary key and set auto_increment on one of these. See below the code for issues that occur.

/****
 * @Entity
 */
class User {
    /****
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /****
     * @Column(type="string")
     */
    private $name;

    /****
     * @OneToMany(targetEntity="PhoneNumber",mappedBy="id",cascade={"all"})
     */
    private $phoneNumbers;

    public function setName ($name) {
        $this->name = $name;
    }
}
/****
 * @Entity
 */
class PhoneNumber {
    /****
     * @Id
     * @Column(type="integer")
     * @GeneratedValue(strategy="AUTO")
     */
    private $id;

    /****
     * @Id
     * @ManyToOne(targetEntity="User",cascade={"all"})
     */
    private $user;

    /****
     * @Column(type="string")
     */
    private $phonenumber;

    public function setUser (User $user) {
        $this->user = $user;
    }

    public function setPhoneNumber ($phoneNumber) {
        $this->phonenumber = $phoneNumber;
    }
}
$albert = new User;
$albert->setName("albert");
$em->persist($albert);

$phoneAlbert1 = new PhoneNumber();
$phoneAlbert1->setUser($albert);
$phoneAlbert1->setPhoneNumber("albert home: 012345");
$em->persist($phoneAlbert1);

The first issue which occurs is that Doctrine does not generate the field "id" within PhoneNumber set to auto_increment.

The second issue which occurs is that Doctrine becomes confused when inserting a new record into PhoneNumber, because of the following INSERT INTO statement:

INSERT INTO PhoneNumber (user_id, phonenumber) VALUES (?, ?)
array(1) {
  [1]=>
  string(19) "albert home: 012345"
}

SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens
Originally created by @doctrinebot on GitHub (Nov 25, 2010). Jira issue originally created by user felicitus: Given the following entity definitions, Doctrine does not attempt to manage generated values. For example, in MySQL, it is perfectly possible to create a composite primary key and set auto_increment on one of these. See below the code for issues that occur. ``` /**** * @Entity */ class User { /**** * @Id * @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ private $id; /**** * @Column(type="string") */ private $name; /**** * @OneToMany(targetEntity="PhoneNumber",mappedBy="id",cascade={"all"}) */ private $phoneNumbers; public function setName ($name) { $this->name = $name; } } ``` ``` /**** * @Entity */ class PhoneNumber { /**** * @Id * @Column(type="integer") * @GeneratedValue(strategy="AUTO") */ private $id; /**** * @Id * @ManyToOne(targetEntity="User",cascade={"all"}) */ private $user; /**** * @Column(type="string") */ private $phonenumber; public function setUser (User $user) { $this->user = $user; } public function setPhoneNumber ($phoneNumber) { $this->phonenumber = $phoneNumber; } } ``` ``` $albert = new User; $albert->setName("albert"); $em->persist($albert); $phoneAlbert1 = new PhoneNumber(); $phoneAlbert1->setUser($albert); $phoneAlbert1->setPhoneNumber("albert home: 012345"); $em->persist($phoneAlbert1); ``` The first issue which occurs is that Doctrine does not generate the field "id" within PhoneNumber set to auto_increment. The second issue which occurs is that Doctrine becomes confused when inserting a new record into PhoneNumber, because of the following INSERT INTO statement: ``` INSERT INTO PhoneNumber (user_id, phonenumber) VALUES (?, ?) array(1) { [1]=> string(19) "albert home: 012345" } SQLSTATE[HY093]: Invalid parameter number: number of bound variables does not match number of tokens ```
admin added the New Feature label 2026-01-22 13:02:25 +01:00
Author
Owner

@doctrinebot commented on GitHub (Nov 25, 2010):

Comment created by romanb:

I don't think this will ever be possible.

@doctrinebot commented on GitHub (Nov 25, 2010): Comment created by romanb: I don't think this will ever be possible.
Author
Owner

@doctrinebot commented on GitHub (Nov 25, 2010):

Comment created by felicitus:

Is there a technical reason for that? I mean, with DDC-117 we are aiming for composite foreign keys, or is DDC-117 cancelled?

@doctrinebot commented on GitHub (Nov 25, 2010): Comment created by felicitus: Is there a technical reason for that? I mean, with [DDC-117](http://www.doctrine-project.org/jira/browse/DDC-117) we are aiming for composite foreign keys, or is [DDC-117](http://www.doctrine-project.org/jira/browse/DDC-117) cancelled?
Author
Owner

@doctrinebot commented on GitHub (Nov 25, 2010):

Comment created by @beberlei:

A composite key is ALWAYS of the type "ASSIGNED" and cannot be a combination of different id generation strategies.

You could however write a prePersist Listener that does this for you.

@doctrinebot commented on GitHub (Nov 25, 2010): Comment created by @beberlei: A composite key is ALWAYS of the type "ASSIGNED" and cannot be a combination of different id generation strategies. You could however write a prePersist Listener that does this for you.
Author
Owner

@doctrinebot commented on GitHub (Nov 25, 2010):

Comment created by felicitus:

Okay, maybe this is a feature for 3.0 or so. However, I'd suggest leaving this bug open as this is something which needs to be documented once DDC-117 becomes integrated within the main branch.

Additionally, Doctrine should complain about different ID generation strategies. Right now it silently ignores it.

@doctrinebot commented on GitHub (Nov 25, 2010): Comment created by felicitus: Okay, maybe this is a feature for 3.0 or so. However, I'd suggest leaving this bug open as this is something which needs to be documented once [DDC-117](http://www.doctrine-project.org/jira/browse/DDC-117) becomes integrated within the main branch. Additionally, Doctrine should complain about different ID generation strategies. Right now it silently ignores it.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: doctrine/archived-orm#1105