DDC-1121: Doctrine CLI tool produces wrong schema when the id columns of the entities are not named as $id. The annotations block aren't taken into account #1404

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

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

Originally assigned to: @beberlei on GitHub.

Jira issue originally created by user screwyprof:

namespace Entities;

//use Doctrine\Common\Collections\ArrayCollection;

/**** 
 *@Entity
 *@Table(name="user")
 */
class User
{
    /**** 
    *@Id 
    *@Column(type="integer", name="user_id")
    *@GeneratedValue
    ** **/
    private $userId;

    /*** @Column(length=255) **/
    private $lastName;

    /*** @Column(length=255, name="user_firstname") **/
    private $firstName;

     /****
     * Bidirectional - Many users are geathered within one group (OWNING SIDE)
     *
     * @ManyToOne(targetEntity="\Entities\Group", inversedBy="user")
     */
    private $group;

/// ---
}
<?php
namespace Entities;
/**** 
 *@Entity
 *@Table(name="group")
 */
class Group
{
    /**** 
    *@Id 
    *@Column(type="integer", name="group_id")
    *@GeneratedValue
    ** **/
    private $groupId;

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

    /****
     * @OneToMany(targetEntity="\Entities\User", mappedBy="group")
     */
    private $users;

    public function **construct() {
        $this->users = new \Doctrine\Common\Collections\ArrayCollection();
    }
}

In the following examples the id fields are set to $userId (mapped to user_id) and $groupId (mapped to group_id), but when it comes to generating the schema an error occures. The Doctrine CLI Tool doesn't work properly. When it generates the schema it fails to take into account the annotation blocks and just uses "id" instead of "group_id".

Let me explain it better. If we run the tool it will produce the following code

...

ALTER TABLE user ADD FOREIGN KEY (group_id) REFERENCES group(id)

instead of

ALTER TABLE `user`  ADD  FOREIGN KEY (`group*id`) REFERENCES `groups` (`group*id`);

Moreover using "group" as a name of the entity class will lead to numerous problems, because it's a reserved word in SQL. Even if we specify the quoatation forcibly using backticks it won't work:

 /***@Table(name="`group`")**/
Class Group {

The quoatation won't be taken into account while generation the schema.

I'm enclosing the files I've tried to play with. Just try to generate the schema.

Originally created by @doctrinebot on GitHub (Apr 20, 2011). Originally assigned to: @beberlei on GitHub. Jira issue originally created by user screwyprof: ``` namespace Entities; //use Doctrine\Common\Collections\ArrayCollection; /**** *@Entity *@Table(name="user") */ class User { /**** *@Id *@Column(type="integer", name="user_id") *@GeneratedValue ** **/ private $userId; /*** @Column(length=255) **/ private $lastName; /*** @Column(length=255, name="user_firstname") **/ private $firstName; /**** * Bidirectional - Many users are geathered within one group (OWNING SIDE) * * @ManyToOne(targetEntity="\Entities\Group", inversedBy="user") */ private $group; /// --- } ``` ``` <?php namespace Entities; /**** *@Entity *@Table(name="group") */ class Group { /**** *@Id *@Column(type="integer", name="group_id") *@GeneratedValue ** **/ private $groupId; /*** @Column(name="group_name") **/ private $name; /**** * @OneToMany(targetEntity="\Entities\User", mappedBy="group") */ private $users; public function **construct() { $this->users = new \Doctrine\Common\Collections\ArrayCollection(); } } ``` In the following examples the id fields are set to $userId (mapped to user_id) and $groupId (mapped to group_id), but when it comes to generating the schema an error occures. The Doctrine CLI Tool doesn't work properly. When it generates the schema it fails to take into account the annotation blocks and just uses "id" instead of "group_id". Let me explain it better. If we run the tool it will produce the following code ... ``` ALTER TABLE user ADD FOREIGN KEY (group_id) REFERENCES group(id) ``` instead of ``` ALTER TABLE `user` ADD FOREIGN KEY (`group*id`) REFERENCES `groups` (`group*id`); ``` Moreover using "group" as a name of the entity class will lead to numerous problems, because it's a reserved word in SQL. Even if we specify the quoatation forcibly using backticks it won't work: ``` /***@Table(name="`group`")**/ Class Group { ``` The quoatation won't be taken into account while generation the schema. I'm enclosing the files I've tried to play with. Just try to generate the schema.
admin added the Bug label 2026-01-22 13:13:20 +01:00
admin closed this issue 2026-01-22 13:13:21 +01:00
Author
Owner

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

Comment created by @beberlei:

Fixed formatting, scheduled for 2.0.5

@doctrinebot commented on GitHub (Apr 30, 2011): Comment created by @beberlei: Fixed formatting, scheduled for 2.0.5
Author
Owner

@doctrinebot commented on GitHub (May 1, 2011):

Comment created by @beberlei:

Just understood this. This is expected behavior, "id" is the default value. Doctrine cannot - for optimization reasons - check the id column names on the other entity. That is what the @JoinColumn annotation is for, where you can explicitly give a name.

@doctrinebot commented on GitHub (May 1, 2011): Comment created by @beberlei: Just understood this. This is expected behavior, "id" is the default value. Doctrine cannot - for optimization reasons - check the id column names on the other entity. That is what the @JoinColumn annotation is for, where you can explicitly give a name.
Author
Owner

@doctrinebot commented on GitHub (May 1, 2011):

Issue was closed with resolution "Invalid"

@doctrinebot commented on GitHub (May 1, 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#1404