TheDeveloo
TheDeveloo

Reputation: 432

CLI Doctrine 2 - index already defined

I'm trying to create my database with Doctrine 2 from my PHP entity.

Here is my code from the class Team :

<?php
// Team.php
/**
 * @Entity @Table(name="team")
 **/
class Team
{
    /**
     * @Id
     * @OneToOne(targetEntity="User")
     * @JoinColumn(name="userID", referencedColumnName="id")
     */
    protected $user;

    /**
     * @Column(type="string",length=30)
     * @var string
     **/
    protected $function;

    /**
     * @Column(type="text")
     * @var string
     **/
    protected $description;

    /**
     * @OneToOne(targetEntity="File")
     * @JoinColumn(name="fileID", referencedColumnName="id")
     */
    protected $img;

    /**
     * @OneToOne(targetEntity="File")
     * @JoinColumn(name="fileID", referencedColumnName="id")
     */
    protected $cv;

    /**
     * @Id
     * @OneToOne(targetEntity="Language")
     * @JoinColumn(name="languageID", referencedColumnName="id")
     */
    protected $lang;

    public function getUser()
    {
        return $this->user;
    }

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

    public function getFunction()
    {
        return $this->function;
    }

    public function setFunction($function)
    {
        $this->function = $function;
    }

    public function getDescription()
    {
        return $this->description;
    }

    public function setDescription($description)
    {
        $this->description = $description;
    }

    public function getImg()
    {
        return $this->img;
    }

    public function setImg($img)
    {
        $this->img = $img;
    }

    public function getCv()
    {
        return $this->cv;
    }

    public function setCv($cv)
    {
        $this->cv = $cv;
    }

    public function getLang()
    {
        return $this->lang;
    }

    public function setLang(Language $language)
    {
        $this->lang = $language;
    }
}

And the error :

[Doctrine\DBAL\Schema\SchemaException]
An index with name 'uniq_c4e0a61f93cb796c' was already defined on table 'team'

Doctrine is loaded from composer and used with Windows CMD (if that can help).

I've seen that an issue has been reported for v.2.5.0 so I loaded 2.4.7 but same error, so I tried dev-master but still the same.

I also tried removing the composite Id and replaced it by a simple generated @Id, or even none (Doctrine was then saying No identifier/primary key specified for Entity "Team").

This code was working with v.2.4.2 but the point is to update the tools used for this project.

Do somebody know how I could do this ?

Upvotes: 4

Views: 3712

Answers (2)

TheDeveloo
TheDeveloo

Reputation: 432

Well well, I found the problem :

/**
 * @OneToOne(targetEntity="File")
 * @JoinColumn(name="fileID", referencedColumnName="id")
 */
protected $img;

/**
 * @OneToOne(targetEntity="File")
 * @JoinColumn(name="fileID", referencedColumnName="id")
 */
protected $cv;

More precisely @JoinColumn(name="fileID", referencedColumnName="id"), the column name fileID is the same for both.

Thanks anyway !

Upvotes: 11

Mark
Mark

Reputation: 5777

You problem lies here:

/**
 * @Id
 * @OneToOne(targetEntity="Language")
 * @JoinColumn(name="languageID", referencedColumnName="id")
 */
protected $lang;

You have to remove the @Id from your $lang property. Associations should not be marked as entity identifiers.

The @Id annotation is used to mark the primary key (http://doctrine-orm.readthedocs.org/en/latest/reference/annotations-reference.html#annref-id). Your association is not a primary key, when migrating the schema doctrine will attempt to add an index and map a foreign key (depending on the configuration).

Since you marked the association as an identifier this conflicts with your $id key.

Upvotes: 0

Related Questions