Topic: Test foreign key constraints?
I have Accounts and People in my database. An Account belongs_to a Person through the owner_id column and I have a foreign key constraint in my database between Account.owner_id and Person.id
If I run a ruby script/console then when I try to create an account that has an owner_id that is not in the database, it fails with the appropriate error:
>> account1.owner_id = 123
ActiveRecord::StatementInvalid: Mysql::Error: Cannot add or update a child row: a foreign key constraint fails
It fails because there is no Person in the database with id=123. However when I try to do the exact same thing in my tests it succeeds. Why is that? Is there something I need to do to turn on foreign key constraints in the test environment?
No error produced here and the test fails (length = 1):
account = Account.new(:owner_id => 123)
account.save # This doesn't produce an error?
assert Account.find(:all).length == 0
Last edited by saracen (2007-07-11 21:45:17)