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
end
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’
ActiveRecord::Base.establish_connection(RAILS_ENV.to_sym)
[
:accounts,
:customers,
:jobs
].each{|f| Fixtures.create_fixtures(‘test/fixtures’, f) }
end