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;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?