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: [email protected]
[mailto:[email protected]] On Behalf Of M Daggett
Sent: Tuesday, 22 November 2005 12:04 p.m.
To: [email protected]
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:
- Error:
test_create(CategoryTest):
ActiveRecord::StatementInvalid: #23000Cannot delete or update a parent
row: a foreign key constraint fails (itsthes_test/categories
,
CONSTRAINTfk_ 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. [email protected] 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
cheers
–
Posted via http://www.ruby-forum.com/.
Rails mailing list
[email protected]
http://lists.rubyonrails.org/mailman/listinfo/rails
–
I am Mark Daggett and I approve this message.