New to Ruby on Rails? Spread the Word!: DIGG This | Add to del.icio.us
Rails411 is your one stop Ruby on Rails resource directory. Here you will find Ruby on Rails tutorials, Conference & Workshop Listings, Forums, and Hosting. We are the definitive guide for Ruby on Rails web applications, components, gems, scripts, and other related resources for Ruby on Rails. Accomplishing more with your Ruby on Rails website is easy with the help of these tested, pre-packaged, and ready-to-run resources.

Click any category to add your site for free.

Friday, January 9 2009

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