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 #1719

Closed
doctrinebot opened this Issue Apr 20, 2011 · 3 comments

2 participants

@doctrinebot

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 userid) 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 "groupid".

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.

@doctrinebot

Comment created by @beberlei:

Fixed formatting, scheduled for 2.0.5

@doctrinebot

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

Issue was closed with resolution "Invalid"

@beberlei beberlei was assigned by doctrinebot Dec 6, 2015
@doctrinebot doctrinebot added this to the 2.0.5 milestone Dec 6, 2015
@doctrinebot doctrinebot closed this Dec 6, 2015
@doctrinebot doctrinebot added the Bug label Dec 7, 2015
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment