# Testing Referential Integrity

**URL:** <https://www.ruby-forum.com/t/testing-referential-integrity/176255>\
**Category:** Rails\
**Created:** [September 28, 2009, 5:45am UTC](https://www.ruby-forum.com/t/testing-referential-integrity/176255 "2009-09-28T05:45:23Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [September 28, 2009, 5:45am UTC](https://www.ruby-forum.com/t/testing-referential-integrity/176255/1 "2009-09-28T05:45:23Z")

</div>

Hey all,

I am having a little trouble getting some referential integrity tests  
to work well.

Right now, I am using Postgres as my database. I have generated  
migrations for my tables and ensured referential integrity by using  
execute commands. All is working well at the level of the database  
and application. I can get all the checks to work flawlessly if I set  
up my test database by issuing “rake db:migrate RAILS\_ENV=test”.

However, if I do the more traditional approach by issuing “rake  
db:test:prepare and rake db:test:load,” this seems to load the  
information from the schema.rb file. This file does not seem to  
contain the foreign key constraints (or other db constraints, for that  
matter).

Any thoughts on getting this to work properly and getting the  
schema.rb file to recognize all the db checks?

I can imagine that this will be an issue at the time of production,  
because the schema.rb file will not be usable for setting up the  
production database. It seems to me, I would have to run the  
migrations…

Thanks,

Andrew

---

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [September 28, 2009, 9:34am UTC](https://www.ruby-forum.com/t/testing-referential-integrity/176255/2 "2009-09-28T09:34:50Z")

</div>

On Sep 28, 4:44 am, Andrew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> However, if I do the more traditional approach by issuing “rake  
> db:test:prepare and rake db:test:load,” this seems to load the  
> information from the schema.rb file. This file does not seem to  
> contain the foreign key constraints (or other db constraints, for that  
> matter).

The ruby schema dumper doesn’t understand things that activerecord  
doesn’t, which includes foreign key constraints (There’s a plugin  
somewhere that adds foreign key support to active record). One way  
around this is to set the schema dumper format (in environment.rb)  
to :sql rather than ruby. Instead of generating schema.rb,  
RAILS\_ENV.sql will be generated, using your database’s tools.

Fred

---

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [September 28, 2009, 12:35pm UTC](https://www.ruby-forum.com/t/testing-referential-integrity/176255/3 "2009-09-28T12:35:36Z")

</div>

Andrew P. wrote:  
[…]

> I can imagine that this will be an issue at the time of production,  
> because the schema.rb file will not be usable for setting up the  
> production database.

What do you mean, “at time of production”? Are you _really_ planning to  
deploy to production only after everything is done? If so, you’re  
setting yourself up for integration hell. You should be deploying to a  
production server (or at least a staging server) quite frequently.  
Don’t wait.

> It seems to me, I would have to run the  
> migrations…

You _should_ be running the migrations as you deploy each new version.  
(But you’re right, that’s not how you should set up the DB from  
scratch.)

> Thanks,
> 
> Andrew

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![diabolist](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/c2a13f/32.png) [@diabolist](https://www.ruby-forum.com/u/diabolist)\
**Post date:** [September 28, 2009, 12:29pm UTC](https://www.ruby-forum.com/t/testing-referential-integrity/176255/4 "2009-09-28T12:29:42Z")

</div>

Frederick C. wrote:

> On Sep 28, 4:44ï¿½am, Andrew P. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:
> 
> > However, if I do the more traditional approach by issuing “rake  
> > db:test:prepare and rake db:test:load,” this seems to load the  
> > information from the schema.rb file. ï¿½This file does not seem to  
> > contain the foreign key constraints (or other db constraints, for that  
> > matter).
> 
> The ruby schema dumper doesn’t understand things that activerecord  
> doesn’t, which includes foreign key constraints (There’s a plugin  
> somewhere that adds foreign key support to active record).

That would be foreign\_key\_migrations. You should _definitely_ be using  
this rather than writing your foreign key constraints in execute  
statements.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid
