Symfony fixture & many to many relationship (Doctrine)
I am trying to create fixtures on a table that has a many to many relationship, but I am getting this error:
SQLSTATE[23000]: Integrity constraint violation: 1452 Cannot add or update a child row: a foreign key constraint fails (
bambinbazar
.bambinbazar_category_articles
, CONSTRAINTbabi
FOREIGN KEY (articles_id
) REFERENCESbambinbazar_articles
(id
) ON DELETE CASCADE)
In my fixture :
BambinbazarArticles:
article_vetement开发者_JAVA百科:
title: bla bla
BambinbazarCategory: [whateverCat]
In my schema:
relations:
BambinbazarCategory:
class:BambinbazarCategory
refClass: BambinbazarCategoryArticles
local: category_id
foreign: articles_id
foreignAlias: BambinbazarArticles
You must create a BambinbazarCategory object with a fixture first (whatevercat does not reference anything, does it?)
You need to load the fixtures in such an order that doesn't try to create a "child" record before a "parent" record for it exists in the database. When you load all the fixtures at once, it's creating illegal foreign-key referenced fields so it throws that error.
Maybe there's some clever way around it but I personally just load the fixtures separately, starting from the parent, by taking the fixture files out of the fixtures folder, running the standard load-data command, sticking the other fixture file back in, and repeating the command.
精彩评论