Doctrine自动生成的迁移无法识别外键约束

时间:2015-02-18 14:32:11

标签: php mysql symfony doctrine-orm doctrine-migrations

通过命令doctrine:migrations:diff创建迁移时,我得到一个之后无法执行的迁移。

我有以下情况:
我有2个entites(parentchild),彼此之间有many to many的关系。因此,doctrine为它生成了一个映射表parents_childs。目前,这2个entiteis将id作为整数,现在我想将类型更改为bigint

当我创建迁移时,我会得到这样的结果:

$this->addSql('ALTER TABLE child CHANGE id id BIGINT UNSIGNED NOT NULL');
$this->addSql('ALTER TABLE parent CHANGE id id BIGINT UNSIGNED NOT NULL');
$this->addSql('ALTER TABLE parents_childs CHANGE parent_id parent_id BIGINT UNSIGNED NOT NULL, CHANGE child_id child_id BIGINT UNSIGNED NOT NULL');

执行迁移时:

  

SQLSTATE [HY000]:常规错误:1025重命名时出错   './symfony_dev/#sql-379_34'到'./symfony_dev/oauth_client'(错误号码:   150)

SHOW ENGINE INNODB STATUS ;给出了以下关于约束错误的消息:

  

------------------------最新外国关键错误   ------------------------ 150218 14:28:19表my_db / parents_childs的外键约束出错:有   引用表中没有包含列的索引   第一列或引用表中的数据类型不匹配   表中的那些。约束:,CONSTRAINT“FK_98FFA0B4D395B25E”   FOREIGN KEY(“parent_id”)REFERENCES“parents”(“id”)索引   表中的外键是“PRIMARY”   http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html   正确的外键定义。

父实体定义:

AcmeBundle\Entity\Parent:
    type: entity
    table: parent
    id:
        id:
            type: bigint
            unique: true
            nullable: false
            generator:
                strategy: CUSTOM
            customIdGenerator:
                class: 'MyCustomIdGenerator'
            options:
                unsigned: true
    fields: ...
    manyToMany:
        parents:
            targetEntity: AcmeBundle\Entity\Childs
            joinTable:
                name: parents_childs
                joinColumns:
                    parent_id:
                        referencedColumnName: id
                inverseJoinColumns:
                    child_id:
                        referencedColumnName: id
                        unique: true
            cascade: [remove, persist]
            orphanRemoval: true
            inversedBy: parents

儿童实体定义:

AcmeBundle\Entity\Child:
    type: entity
    table: child
    id:
        id:
            type: bigint
            unique: true
            nullable: false
            generator:
                strategy: CUSTOM
            customIdGenerator:
                class: 'MyCustomIdGenerator'
            options:
                unsigned: true
    fields: ...
    manyToMany:
        parents: 
            targetEntity: AcmeBundle\Entity\Parent
            mappedBy: parents

删除外键,执​​行更新并再次添加它会很有效,但实际上我不想手动操作,因为它也会影响更多的表。
所以Iam现在不确定Doctrine是否能够正确识别外键或者我做错了什么?

0 个答案:

没有答案