| Rails411 |
| Disable Foreign Key Checks Under MySql |
|
When you’ve built a database that includes foreign key constraints between parent and child tables, loading fixtures using rake load_fixtures and unit testing can fail with errors such as: Mysql::Error: #23000Cannot add or update a child row: a foreign key constraint fails (`depot_development/line_items`, CONSTRAINT `fk_items_product` FOREIGN KEY (`product_id`) REFERENCES `products` (`id`) ON DELETE CASCADE): INSERT INTO line_items (`quantity`, `product_id`, `id`, `unit_price`) VALUES (1, 1, 1, 29.95) OR ActiveRecord::StatementInvalid: Mysql::Error: Cannot delete or update a parent row: a foreign key constraint fails: DELETE FROM line_items WHERE id = 1 ... because the child record is being inserted into the database before the parent record. A similar error can occur when updating fixtures using the same rake task because the old parent record is being deleted before the child record. |
| Listing Information | ||||
|---|---|---|---|---|
| Website: | http://wiki.rubyonrails.com/rails/pages/DisableForeignKeyChecksUnderMySql | |||
| Average Visitor Rating: | 1.00 (Out of 5) | |||
| Number of ratings: | 1 | |||
| Hits: | 519 | |||
| Added: | 2006-05-30 13:15:25 | |||
| Last updated: | 2006-07-17 12:48:27 | |||