Testing with foreign keys

I’m looking for a plugin/whatever for preloading fixtures, to run
tests with FKs defined.

Specifying load order just doesn’t work for me, I need to purge the
test db, load all fixtures, and only then create the FK constraints.

Any suggestions?


On 11/10/06, Isak H. [email protected] wrote:



I use the foreign key support offered by the redhillonrails_core plugin.

Redhill plugins:

redhillonrails_core plugin svn repo:

To run tests I just clone the development database.

$ rake db:test:clone_structure

Now your test database has all your foreign keys. Just specify your
fixtures in order (in your tests) and you shouldn’t have any problems.
For example if a customer belongs to an account and has a foreign key
contraint customers(account_id) to accounts(id) then the following
order will load the fixtures fine.

class CustomersControllerTest < Test::Unit::TestCase
fixtures :accounts, :customers

If you really want to preload all the fixtures then just make a custom
rake task:

desc “Load fixtures into the current environment’s database in order”
task :load => :environment do
require ‘active_record/fixtures’
].each{|f| Fixtures.create_fixtures(‘test/fixtures’, f) }

Hope this helps.

Zack C.

What we’re doing is define all the fixtures in test_helper.rb so you
don’t have to define them in each test file.