# RE: Re: challenge

**URL:** <https://www.ruby-forum.com/t/re-re-challenge/51773>\
**Category:** Rails\
**Created:** [November 22, 2005, 12:48am UTC](https://www.ruby-forum.com/t/re-re-challenge/51773 "2005-11-22T00:48:11Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![jviney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jviney](https://www.ruby-forum.com/u/jviney)\
**Post date:** [November 22, 2005, 12:48am UTC](https://www.ruby-forum.com/t/re-re-challenge/51773/1 "2005-11-22T00:48:11Z")

</div>

I have run in to the same problem. It happens because the individual  
fixtures aren’t loaded in the order they are declared in the YAML file.  
I tracked this down to the fact that YAML::load simply returns a hash of  
all fixtures in the file. A very simple way to fix this (if your  
fixtures are declared in order of id) is to sort this hash before AR  
tries to load it, therefore making sure the fixtures are loaded in the  
correct order.

Change line 283 in lib/active\_record/fixtures.rb to sort the fixtures  
before inserting them:

values.sort { |a, b| a[“id”].\<=\>(b[“id”]) }.each do |fixture|

So they will inserted in order of id.

That partially fixes the problem, although there are still issues  
surrounding foreign keys/testing/fixtures.

Cheers, -Jonny.

* * *

From: removed\_email\_address@domain.invalid  
[[mailto:removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)] On Behalf Of M Daggett  
Sent: Tuesday, 22 November 2005 12:04 p.m.  
To: removed\_email\_address@domain.invalid  
Subject: Re: [Rails] Re: challenge

I am having a similar problem, I have a category model which is defined  
to “acts\_as\_tree”. The table structure looks like this:

create table categories (  
id INT not null auto\_increment,  
name VARCHAR(200) not null,  
parent\_id int,  
description TEXT ,  
created\_on DATETIME null,  
updated\_on DATETIME null,  
constraint fk\_categories\_to\_categories foreign key (parent\_id)  
references categories(id),  
primary key (id)  
);

as you can see the foreign key is a reference to another row in the  
table. For the root category I just have (NULL) specified for the  
parent\_id. However all of my unit tests are blowing up because it cannot  
find the parent category for the root category (because it does not  
exist).

## ERROR MESSAGE:

1. Error:  
test\_create(CategoryTest):  
ActiveRecord::StatementInvalid: #23000Cannot delete or update a parent  
row: a foreign key constraint fails (`itsthes_test/categories`,  
CONSTRAINT `fk_ categories_to_categories` FOREIGN KEY (`parent_id`) REFERENCES  
`categories` (`id`)): DELETE FROM categories

* * *

Does anyone have an idea how to solve this problem?

Thanks!  
Mark

On 11/14/05, Joe B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:  
thanks all the guys reply my “challenge”.  
and “liquid” give us a good link  
it’s solve this kind of problem.  
here:  
[http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship](http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship)

cheers

–  
Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).

* * *

Rails mailing list  
removed\_email\_address@domain.invalid  
[http://lists.rubyonrails.org/mailman/listinfo/rails](http://lists.rubyonrails.org/mailman/listinfo/rails)

## –

I am Mark Daggett and I approve this message.

---

<div class="post-metadata">

**Author:** ![jviney](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/779978/32.png) [@jviney](https://www.ruby-forum.com/u/jviney)\
**Post date:** [November 22, 2005, 4:29am UTC](https://www.ruby-forum.com/t/re-re-challenge/51773/2 "2005-11-22T04:29:20Z")

</div>

Hey Jonathan,  
You are exactly right. It seems the real trick is not to just use a  
structured YAML file but to also inform the test how to load and remove  
records for use in the fixture. Right now my tests are all breaking  
because  
I have one table of images that contains a foreign key to its respective  
category. The category in turn has a key to its parent. When the  
fixtures  
run for the first test it creates the images but when it comes time to  
delete the categories and create them again for the next test it bombs  
because of the image’s dependencies implicit in the foreign key  
relationship.

Seems pretty tricky but at the same time foreign keys seems so obvious  
and  
necessary that the fact that I am struggling at it makes me feel I am  
using  
rails wrong (especially since it is so database centric). I am using  
MYSQL  
and I am wondering if I might have less problems using POSTGRES or  
something. Thanks for your tip, I’ll try it out and report back.

Mark
