# Testing with foreign keys

**URL:** <https://www.ruby-forum.com/t/testing-with-foreign-keys/75516>\
**Category:** Rails\
**Created:** [November 10, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/testing-with-foreign-keys/75516 "2006-11-10T16:24:40Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [November 10, 2006, 4:24pm UTC](https://www.ruby-forum.com/t/testing-with-foreign-keys/75516/1 "2006-11-10T16:24:40Z")

</div>

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?

Isak

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [November 10, 2006, 6:49pm UTC](https://www.ruby-forum.com/t/testing-with-foreign-keys/75516/2 "2006-11-10T18:49:02Z")

</div>

On 11/10/06, Isak H. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Isak
> 
> >

Isak,

I use the foreign key support offered by the redhillonrails\_core plugin.

Redhill plugins:  
[http://www.redhillconsulting.com.au/rails\_plugins.html](http://www.redhillconsulting.com.au/rails_plugins.html)

redhillonrails\_core plugin svn repo:  
svn://rubyforge.org/var/svn/redhillonrails/trunk/vendor/plugins/redhillonrails\_core

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

Hope this helps.

–  
Zack C.  
[http://depixelate.com](http://depixelate.com)

---

<div class="post-metadata">

**Author:** ![Isak\_H](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/i/77aa72/32.png) [@Isak\_H](https://www.ruby-forum.com/u/Isak_H)\
**Post date:** [November 12, 2006, 5:53am UTC](https://www.ruby-forum.com/t/testing-with-foreign-keys/75516/3 "2006-11-12T05:53:18Z")

</div>

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.
