Technology and Software

PostgreSQL vs. Rails’s fixtures

Rails’ fixtures don’t always work well. With PostgreSQL I’ve been experiencing a kind of deadlock, where I can’t say neither fixture :t1, :t2 nor fixture :t2, :t1. I’m using foreign keys that refer from :t1 to :t2, a choice that Rails doesn’t look to like much. I’m getting a runtime error anyway: either t1 can’t be populated before t2, because the elements of t2 must be there in advance; or t2 can’t be deleted because t1 has records that refers to it. Rails can’t figure out the appropriate way to load and delete fixtures, even if the documentation is pretty clear that it should with the right order of fixture declarations. Well, it doesn’t. It should be a bug, but how to work around it? Luckily it’s simple, even if I don’t like it much: enabling ON DELETE CASCADE on each foreign key fixes it.
By the way, this is the first time I run into this problem and it’s the first time that I’m using config.active_record.schema_format = :sql instead of config.active_record.schema_format = :db. I can’t easily go back to :db and check, but it might be that the sql schema format doesn’t let Rails grock the db structure well.

Advertisements
Standard

One thought on “PostgreSQL vs. Rails’s fixtures

  1. Well, DHH works with Mysql, which traditionally does not support referential integrity. Yes I know that InnoDB tables do support ref. integrity, but I guess most Railers do not bother to apply the constraints to their db schema. If you do implement constraints, the simple text-file based fixture system may not work well for you.

    One workaround could be to declare the foreign key column nullable. Then you have to set up the foreign keys in your setup methods. One other way is to do away with fixture files altogether and load all fixture data within setup methods.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s