Forum: Ruby on Rails PostgreSQL 8.1, testing with constraints

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
Pat M. (Guest)
on 2005-11-30 20:20
(Received via mailing list)
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
NOTICE:  CREATE TABLE will create implicit sequence "users_id_seq1"
for serial column ""
`log': ERROR:  relation "users" already exists

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 M. (Guest)
on 2005-11-30 20:49
(Received via mailing list)
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:
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".

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?

This topic is locked and can not be replied to.