PostgreSQL 8.1, testing with constraints

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

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