# PostgreSQL 8.1, testing with constraints

**URL:** <https://www.ruby-forum.com/t/postgresql-8-1-testing-with-constraints/49156>\
**Category:** Rails\
**Created:** [November 30, 2005, 7:20pm UTC](https://www.ruby-forum.com/t/postgresql-8-1-testing-with-constraints/49156 "2005-11-30T19:20:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [November 30, 2005, 7:20pm UTC](https://www.ruby-forum.com/t/postgresql-8-1-testing-with-constraints/49156/1 "2005-11-30T19:20:34Z")

</div>

I have a users model, and a couple other models that have a foreign  
key reference to the id in the users table. If I run my unit tests  
without the foreign key constraints, they run fine, but blow up when I  
add the constraints.

The first problem is running unit tests on the User model. I get the  
following messages:  
– create\_table(“users”, {:force=\>true})  
NOTICE: constraint user\_id\_fkey on table messages depends on table  
users  
NOTICE: CREATE TABLE will create implicit sequence “users\_id\_seq1”  
for serial column “users.id”  
/usr/local/lib/ruby/gems/1.8/gems/activerecord-1.13.0/lib/active\_record/connection\_adapters/abstract\_adapter.rb:67:in  
`log’: ERROR: relation “users” already exists  
(ActiveRecord::StatementInvalid)

I assume that it’s not able to drop the users table because of a  
foreign key constraint in the messages table. I’m not using the  
messages fixture in these unit tests though, so it shouldn’t matter.

I want to use foreign key constraints in my database, but I also want  
to be able to unit test my code. What am I missing here?

Pat

---

<div class="post-metadata">

**Author:** ![pergesu](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/22d042/32.png) [@pergesu](https://www.ruby-forum.com/u/pergesu)\
**Post date:** [November 30, 2005, 7:49pm UTC](https://www.ruby-forum.com/t/postgresql-8-1-testing-with-constraints/49156/2 "2005-11-30T19:49:05Z")

</div>

I figured out that particular problem, it was because I was using the  
LoginEngine, and the included test\_helper.rb file loads in the user  
schema. This was trying to drop the table, which PostgreSQL obviously  
shouldn’t allow. Use transactional fixtures and don’t load the schema  
and it works fine.

I’m having another problem though, which occurs when I try to use the  
users fixtures in more than one unit test. For example, I have unit  
tests for my Ticket model and my Message model. The Message unit  
tests run fine, but the Ticket tests give me the following error:

1. Error:  
test\_create(TicketTest):  
ActiveRecord::StatementInvalid: ERROR: update or delete on “users”  
violates foreign key constraint “user\_id\_fkey” on “messages”  
DETAIL: Key (id)=(1001) is still referenced from table “messages”.  
: DELETE FROM users

The fixtures line is:  
fixtures :users, :tickets

So the messages fixtures should never be loaded in, so there should  
obviously be no message record that references a user record. Not  
sure why it’s complaining about this. Any ideas?

Pat
