Hi all
I’m quite unsure about this topic, so before hacking some stuff I try to
get some help here.
I use the foreign keys migrations plugin from red hill consulting which
automatically creates foreign keys when using xxx_id in migrations or
specifying them manually.
I set them all to ON DELETE RESTRICT because I want my application to
handle all the dependencies stuff itself and I don’t want it to rely on
the database to delete depending rows. E.g. it would have an
uncontrolled destructive manner if I tried to delete object X that has
dependencies, but MySQL silently kills all dependencies without any
warning. So want to restrict all this and when removing all depending
records the application itself should know about it and do it itself.
But now I have problems with testing. I have a country model and a
dependent music artist model.
The functional tests for the countries only loads fixtures :countries,
so when running them on a completely empty test database the tests work
well.
The functional tests for the music artists load fixtures :countries AND
:music_artists because every music artist has an origin country to
specify. So now I run the functional tests for music artist and they
work well, too.
But after running the tests for music artists I can’t run the tests for
countries anymore, I get errors like:
- Error:
test_show(Admin::CountriesControllerTest):
ActiveRecord::StatementInvalid: Mysql::Error: Cannot delete or update a
parent row: a foreign key constraint fails
(pgbookings_test/music_artists
, CONSTRAINTmusic_artists_ibfk_1
FOREIGN KEY (origin_country_id
) REFERENCEScountries
(id
: DELETE
FROM countries
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract_adapter.rb:128:in
log' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:243:in
execute’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/mysql_adapter.rb:258:in
update' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/connection_adapters/abstract/database_statements.rb:47:in
delete’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:285:in
delete_existing_fixtures' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:256:in
create_fixtures’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:256:in
each' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:256:in
create_fixtures’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:255:in
transaction' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:255:in
create_fixtures’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:248:in
silence' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:248:in
create_fixtures’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:593:in
load_fixtures' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:538:in
setup_with_fixtures’
/usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:575:in
setup' /usr/lib/ruby/gems/1.8/gems/activerecord-1.15.3/lib/active_record/fixtures.rb:574:in
setup’
When examining the back trace I saw the method call
“delete_existing_fixtures”. Now I guess because the tests for countries
only use the :countries fixtures it also only tries to remove the
existing fixtures in this table, and because there are still music
artists from the last tests MySQL complains about dependent rows. Is
that possible?
If so, I guess the only solution would be to force Rails to always
empty the whole database tables for every test before re-populating it
with the wanted fixtures. Might that be?
Thanks a lot for you help, Josh