When I try to reload my fixtures using
php app/console doctrine:fixtures:load
I’m getting this error:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or
update a parent row: a foreign key constraint fails (foo_db.Book, CONSTRAINTFK_C29271DD816C6140FOREIGN KEY (author_id) REFERENCESAuthor(id))
The error is showed when the status "> purging database" is showed.
This is my code:
class Book{
...
/**
* @ORM\ManyToOne(targetEntity="Author", inversedBy="books")
*/
private $author;
...
}
class Author{
...
/**
* @ORM\OneToMany(targetEntity="Book", mappedBy="author")
*/
private $books;
}
More: my boss has the same code and it doesn’t have that error.
Any idea?
sf 2.0.1 (just updated)/ubuntu 10.10.
If I’m guessing correctly, you are using a MySQL database. If yes, then you are facing a bug/problem with the current version of the
doctrine-fixtureslibrary for Doctrine2. The problem is that they are using theTRUNCATEcommand to purge the current database values but this command has problem deleting foreign associations in MySQL.See this issue and this one on the GitHub repository of the library for more information and workarounds.
In my particular case, I run this command from a script, so to make the command work correctly, I do:
This way, the purging is done by the
dropcommand and appending has the same effect as not appending since the database is empty when the fixtures are loaded.I must admit I don’t know why your boss doesn’t have the problem, maybe there is no book associated with an author in his database.
Hope this help.
Regards,
Matt