When creating a migration via the command doctrine:migrations:diff
I get a migration which cannot be executed afterwards.
I have the following scenario:
I got 2 entites (parent
and child
) with a many to many
relation between each other. So doctrine generated a mapping table for it parents_childs
. Currently those 2 entiteis have the id
as integer, now I want to change the type to a bigint
.
When I create the migration I get something like this:
$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');
On execution of the migrations:
SQLSTATE[HY000]: General error: 1025 Error on rename of './symfony_dev/#sql-379_34' to './symfony_dev/oauth_client' (errno: 150)
SHOW ENGINE INNODB STATUS ;
gives me the following message for the constraint errors:
------------------------ LATEST FOREIGN KEY ERROR ------------------------ 150218 14:28:19 Error in foreign key constraint of table my_db/parents_childs: there is no index in referenced table which would contain the columns as the first columns, or the data types in the referenced table do not match the ones in table. Constraint: , CONSTRAINT "FK_98FFA0B4D395B25E" FOREIGN KEY ("parent_id") REFERENCES "parents" ("id") The index in the foreign key in table is "PRIMARY" See http://dev.mysql.com/doc/refman/5.5/en/innodb-foreign-key-constraints.html for correct foreign key definition.
Parent entity definition:
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
Child entity defintion:
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
Dropping the foreign keys, executing the updates and the adding it again would work, tough I actually don't want to do it manually because it also affects a lot more tables.
So Iam now not sure if Doctrine is just not capable of recongizing the foreign keys properly or if I am doing something wrong?