David Carney
David Carney

Reputation: 2210

Ruby on Rails Single Table Inheritance (STI) and unit test problem (with PostgreSQL)

I'm using an STI model with a single "Accounts" table to hold information for Users and Technicians (i.e. User < Account, Technician < Account). Everything works from a functional perspective, but things explode when running unit tests:

... 8) Error: test_the_truth(UserTest): ActiveRecord::StatementInvalid: PGError: ERROR: relation "technicians" does not exist : DELETE FROM "technicians" ...

Essentially, the standard framework doesn't recognize that the Technicians and Users tables (or "relations" as PostgreSQL calls them) don't exist and, in fact, should be aliased to Accounts.

Any ideas? I'm relatively new to RoR and I'm at a loss as how to fix this without ripping out STI all together.

Upvotes: 5

Views: 2912

Answers (3)

Robert Jackson
Robert Jackson

Reputation: 21

I have had a similar issue that was resolved by removing the YAML file for the child model. Essentially rails is looking at the fixtures created in /test/fixtures/ and trying to empty the tables for each so that it can reload them for you.

In my case I had run the script/generate model command which automatically creates a new fixture. Then I changed the model to inherit from the proper parent class. Well, since the fixture still existed, rails was trying to DELETE FROM child before loading the fixtures.

If you really do need to preload data you should use the parent models fixture and set the type field to the proper model name.

Upvotes: 2

David Carney
David Carney

Reputation: 2210

Turns out that the problem was due to the presence of:

./test/fixtures/technicians.yml ./test/fixtures/users.yml

This makes sense as the framework expected to be able to insert data into similarly named tables.

Upvotes: 12

Brian Guthrie
Brian Guthrie

Reputation: 2848

  • Did you make sure your accounts table includes a 'type' column? You need one in order to make STI work.
  • I've actually had some database compatibility trouble with a column called 'type' and have occasionally switched to 'kind' to alleviate that. That might be the problem; try setting self.inheritance_column = "kind" in the base class (Account) and see if that helps.

Upvotes: 0

Related Questions