# Databases allow bad foreign keys from Rails Fixtures

**URL:** <https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222>\
**Category:** Rails\
**Created:** [August 13, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222 "2010-08-13T12:49:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![ferreira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e95f7d/32.png) [@ferreira](https://www.ruby-forum.com/u/ferreira)\
**Post date:** [August 13, 2010, 12:49pm UTC](https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222/1 "2010-08-13T12:49:00Z")

</div>

Hi,

I am using Rails Fixtures to load some test data to my database and  
accidentally I introduced a foreign key out of range.

To my surprise, the database accepted it despite having referential  
integrity constraints (that work). I tried with PostgreSQL and with  
MySQL InnoDB and both allowed.

Example:

Having in the database “Flavours” whith a numeric primary key (id), 5  
entries (1 to 5). I can introduce bad data doing:

Icecream\_1:  
name: my ice cream  
flavour\_id: 6

How is it possible that the fixtures loading go around my database  
constraints?

Thank you.

---

<div class="post-metadata">

**Author:** ![ferreira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e95f7d/32.png) [@ferreira](https://www.ruby-forum.com/u/ferreira)\
**Post date:** [August 13, 2010, 2:10pm UTC](https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222/2 "2010-08-13T14:10:01Z")

</div>

Hi Jaime,

On 13 Aug 2010, at 11:49, Jaime Ferreira wrote:

> How is it possible that the fixtures loading go around my database  
> constraints?

For PostgreSQL AFAIK, the triggers which enforce RI are disabled prior  
to the loading of the fixtures. I only discovered this because I ran  
into a problem recently where my tests were failing because the db user  
for the tests did not have permissions to disable the triggers.

Regards,

Tony Byrne.

---

<div class="post-metadata">

**Author:** ![ferreira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e95f7d/32.png) [@ferreira](https://www.ruby-forum.com/u/ferreira)\
**Post date:** [August 13, 2010, 3:19pm UTC](https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222/3 "2010-08-13T15:19:11Z")

</div>

Thank you Tony.

Mistery solved, looks like there is nothing to worry about, I only have  
to be careful loading the test data.

---

<div class="post-metadata">

**Author:** ![ferreira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e95f7d/32.png) [@ferreira](https://www.ruby-forum.com/u/ferreira)\
**Post date:** [August 13, 2010, 4:17pm UTC](https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222/4 "2010-08-13T16:17:10Z")

</div>

Tony Byrne wrote:

> Hi Jaime,
> 
> On 13 Aug 2010, at 11:49, Jaime Ferreira wrote:
> 
> > How is it possible that the fixtures loading go around my database  
> > constraints?
> 
> For PostgreSQL AFAIK, the triggers which enforce RI are disabled prior  
> to the loading of the fixtures. I only discovered this because I ran  
> into a problem recently where my tests were failing because the db user  
> for the tests did not have permissions to disable the triggers.

Oh, right…I remember seeing that (I think it’s a performance hack?).  
Yet another reason not to use fixtures, arguably the most broken and  
horrific feature of Rails. Jaime, I would _highly_ recommend abandoning  
fixtures and using factories for your testing instead (I like Machinist  
for this). Your tests will be easier to write and more conceptually  
correct.

> Regards,
> 
> Tony Byrne.

## Best,

Marnen Laibow-Koser  
[http://www.marnen.org](http://www.marnen.org)  
removed\_email\_address@domain.invalid

---

<div class="post-metadata">

**Author:** ![ferreira](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/f/e95f7d/32.png) [@ferreira](https://www.ruby-forum.com/u/ferreira)\
**Post date:** [August 13, 2010, 4:56pm UTC](https://www.ruby-forum.com/t/databases-allow-bad-foreign-keys-from-rails-fixtures/193222/5 "2010-08-13T16:56:54Z")

</div>

Thanks Marnen.

Rails has a lot to improve, especially in the relational database area  
but I think it’s in the right way.

I will check out Machinist as you recomend.
