douping3860 2013-07-24 05:25
浏览 56
已采纳

Doctrine2重复键名错误

I'm trying to use doctrine2 migration and schema update with symfony2's console. All my entities are being generated with no problems via:

php app/console doctrine:generate:entities myProject

Also, I am able to create my database and build my schema from scratch with no problems given the following commands:

php app/console doctrine:database:create
php app/console doctrine:schema:create

Whenever I modify, remove, or add in another entity, I always regenerate my entities with:

php app/console doctrine:generate:entities myProject

The problem however, is whenever I want to update my schema I always get the following error:

  [Doctrine\DBAL\DBALException]                                                
  An exception occurred while executing 'CREATE INDEX IDX_F7129A80A76ED395 ON  
   user_user (user_id)':                                                       

  SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name   
  'IDX_F7129A80A76ED395'                                                       






  [PDOException]                                                               
  SQLSTATE[42000]: Syntax error or access violation: 1061 Duplicate key name   
  'IDX_F7129A80A76ED395' 

I have tried doing:

php app/console doctrine:schema:update --force

or

php app/console doctrine:migrations:diff
php app/console doctrine:migrations:migrate

But I always get the same error. What am I doing wrong? Is there any way for me to update my database without having to destroy and build a whole new one every time? I have over 25 entities which consist of large files with many associative mappings (OneToOne, OneToMany, ManyToMany,.. etc) which seems to be causing problems. Any help would be great! Thanks!

On a side note I have a feeling that this piece of code might be causing the problem:

/**
 * @ORM\ ManyToMany(targetEntity="User", inversedBy="myFriends")
 */
protected $friendsWithMe;

/**
 * @ORM\ ManyToMany(targetEntity="User", inversedBy="friendsWithMe")
 * @ORM\ JoinTable(name="friends",
 *      joinColumns={@ORM\ JoinColumn(name="user_id", referencedColumnName="id")},
 *      inverseJoinColumns={@ORM\ JoinColumn(name="friend_user_id", referencedColumnName="id")}
 *      )
 **/
protected $myFriends;

In my user entity, I wanted to build a self-referencing many to many relationship. This is how I'm building out a friends list. Maybe there is something wrong with my logic, but I took this directly off from the doctrine2 documentation on associative mapping.

  • 写回答

1条回答 默认 最新

  • donglun4682 2013-07-24 08:11
    关注

    Not sure it is the cause of the Exception, but there is an error in your ORM mapping. $myFriends is the owning side, $friendsWithMe is the inverse side, so inversedBy attribute should be only present on $friendsWithMe.

    Try to rewrite $myFriendswith mappedBy attribute :

    /**
     * @ORM\ ManyToMany(targetEntity="User", mappedBy="friendsWithMe")
     * @ORM\ JoinTable(name="friends",
     *      joinColumns={@ORM\ JoinColumn(name="user_id", referencedColumnName="id")},
     *      inverseJoinColumns={@ORM\ JoinColumn(name="friend_user_id", referencedColumnName="id")}
     *      )
     **/
    protected $myFriends;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题