i love ROR. but i met a problem : i have a table: users id int not null, name varchar(100)..... and i want create a friends association, seems many-to-many self-referential i create a friends table: user_id int not null, friend_id int not null, constraint fk_fu_user foreign key (user_id) references users(id), constraint fk_fu_friend foreign key (friend_id) references users(id) ..... the user Model contains has_many :friends the friend Model contains belongs_to :user i hope it''s ok ,but when i test it allways: "Couldn''t find Firend without a ID" so, guys, if you know what''s goning on, please give a hand. all regards _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
There is a good article on this on the wiki http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToManyRelationship Maybe this will help Cheers On 11/15/05, allenbobo <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > i love ROR. but i met a problem : > i have a table: > users > id int not null, > name varchar(100)..... > > and i want create a friends association, seems many-to-many > self-referential > > i create a friends table: > user_id int not null, > friend_id int not null, > constraint fk_fu_user foreign key (user_id) references users(id), > constraint fk_fu_friend foreign key (friend_id) references users(id) > ..... > > the user Model contains > has_many :friends > > the friend Model contains > belongs_to :user > > i hope it''s ok ,but when i test it allways: > > "Couldn''t find Firend without a ID" > > so, guys, if you know what''s goning on, please give a hand. > > all regards > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
Hello, Don''t know if this will help or not, but In the Active Record docs there is an ''act as tree'' which works as a one to many on itself. It has allot of info about how this is done in the modal, you could most likely get some ideas from that as well. Chanan Braunstein Knovel Corp. Web Development Manager 607-773-1840 x672 http://www.knovel.com _____ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of Liquid Sent: Monday, November 14, 2005 9:36 AM To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] challenge There is a good article on this on the wiki http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToMan yRelationship <http://wiki.rubyonrails.com/rails/pages/HowToCreateASelfReferentialManyToMa nyRelationship> Maybe this will help Cheers On 11/15/05, allenbobo <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: i love ROR. but i met a problem : i have a table: users id int not null, name varchar(100)..... and i want create a friends association, seems many-to-many self-referential i create a friends table: user_id int not null, friend_id int not null, constraint fk_fu_user foreign key (user_id) references users(id), constraint fk_fu_friend foreign key (friend_id) references users(id) ..... the user Model contains has_many :friends the friend Model contains belongs_to :user i hope it''s ok ,but when i test it allways: "Couldn''t find Firend without a ID" so, guys, if you know what''s goning on, please give a hand. all regards _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails <http://lists.rubyonrails.org/mailman/listinfo/rails> _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
"Couldn''t find Firend without a ID" I hope this is a typo and not copied and pasted from the error! In which case you would have to start looking at where you are trying to reference a "Firend" -note the typo regards, Franc Paul _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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/.
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 Black <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of M Daggett Sent: Tuesday, 22 November 2005 12:04 p.m. To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 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 Black <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
Tada! I answered my own question. I am posting my solution in hopes that he helps other developers. The problem I was facing with the unit tests was that the foreign keys needed to be added and deleted from the fixtures in a particular order. The default YAML format was not helpful in this regard. Fortunately there is such a thing as the ordered mapping language independent type : http://yaml.org/type/omap.html Used in my category example it would look something like this: --- !omap - parent: id: 1 parent_id: NULL title: Parent - child: id: 2 parent_id: 1 title: Child Later, Mark On 11/21/05, M Daggett <heavysixer-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > 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 Black <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > > > -- > -------------------------------------------------------------------- > I am Mark Daggett and I approve this message. >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
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 On 11/21/05, Jonathan Viney <jviney-2wiTBfTs3W7aJNtaN73j0w@public.gmane.org> wrote:> > 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: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto: > rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of M Daggett > Sent: Tuesday, 22 November 2005 12:04 p.m. > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > 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 Black <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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 > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > -------------------------------------------------------------------- > I am Mark Daggett and I approve this message. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
http://dev.rubyonrails.org/ticket/2326 has info on a similar problem. It looks like the best way is to simply disable foreign key checks when doing the deletion (or you may be able to use the tip I gave you, and do a .reverse on the result before deleting the fixtures). It seems MySQL may not have a simple way of deleting the contents of a table with self-referencing foreign key constraints (DELETE FROM table and TRUNCATE table don''t seem to work). -Jonny. ________________________________________ From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of M Daggett Sent: Tuesday, 22 November 2005 4:26 p.m. To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org Subject: Re: [Rails] Re: challenge 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 On 11/21/05, Jonathan Viney <jviney-2wiTBfTs3W7aJNtaN73j0w@public.gmane.org> wrote: 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: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of M Daggett Sent: Tuesday, 22 November 2005 12:04 p.m. To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org 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 Black <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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/HowToCreateASelfReferentialManyT oManyRelationship cheers -- Posted via http://www.ruby-forum.com/. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- -------------------------------------------------------------------- I am Mark Daggett and I approve this message. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails -- -------------------------------------------------------------------- I am Mark Daggett and I approve this message.
Hey Jonny, Thanks for the tip, this is very useful, I may not be reading this correctly but is the poster saying this is a MYSQL specific problem. If I were to migrate to Postgres would my tests pass successfully (well at least this part)? Glad to see it is not a newbie-ish misunderstanding of how things are supposed to work. I read some information on using RAKE to generate data for the database before you run your unit tests. In this case unless I misunderstand it you would not need to include the fixtures into the test. I have bookmarked the ticket and I''ll check back on it as well as trying the fix that is posted on the ticket. Thanks, Mark On 11/21/05, Jonathan Viney <jviney-2wiTBfTs3W7aJNtaN73j0w@public.gmane.org> wrote:> > > http://dev.rubyonrails.org/ticket/2326 has info on a similar problem. It > looks like the best way is to simply disable foreign key checks when > doing the deletion (or you may be able to use the tip I gave you, and do > a .reverse on the result before deleting the fixtures). It seems MySQL > may not have a simple way of deleting the contents of a table with > self-referencing foreign key constraints (DELETE FROM table and TRUNCATE > table don''t seem to work). > > -Jonny. > > ________________________________________ > From: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of M Daggett > Sent: Tuesday, 22 November 2005 4:26 p.m. > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > Subject: Re: [Rails] Re: challenge > > 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 > > On 11/21/05, Jonathan Viney <jviney-2wiTBfTs3W7aJNtaN73j0w@public.gmane.org> wrote: > 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: rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > [mailto:rails-bounces-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org] On Behalf Of M Daggett > Sent: Tuesday, 22 November 2005 12:04 p.m. > To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > 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 Black <allenbobo-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> 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/HowToCreateASelfReferentialManyT > oManyRelationship > > cheers > > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > -------------------------------------------------------------------- > I am Mark Daggett and I approve this message. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > -- > -------------------------------------------------------------------- > I am Mark Daggett and I approve this message. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- -------------------------------------------------------------------- I am Mark Daggett and I approve this message. _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails