Hi everyone, I ran into a serious problem when I was trying to do something similar to twitter follow mechanism. The thing is like this: First, I have a self-referential table, named "users" create_table :users do |t| t.column :name, :string t.column :user_name, :string t.column :password, :string t.column :email, :string end I also have a HABTM join table, named "follow_relations" create_table :follow_realtions do |t| t.column :a_id, :integer # a_id follows b_id t.column :b_id, :integer end My HABTM join is defined as follows has_and_belongs_to_many :followers, :class_name => "User", :join_table => "follow_relations", :foreign_key => "b_id", :association_foreign_key => "a_id" has_and_belongs_to_many :followings, :class_name => "User", :join_table => "follow_relations", :foreign_key => "a_id", :association_foreign_key => "b_id" When I executed: gaoxh = User.create(:name => "gaoxh") micai = User.create(:name => "micai") xiexin = User.create(:name => "xiexin") gaoxh.followers << micai xiexin.followers << micai The first << operation executes the following SQL : INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 1, 3) The second << operation executes the following SQL: INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3) Thus, key id is duplicated. It looks like that every time, the value of column id is copied from column a_id. What''s going on here? How to fix this problem? Thanks in advance, xiahong
Xiahong Gao wrote:> Hi everyone, > > I ran into a serious problem when I was trying to do something similar > to twitter follow mechanism. The thing is like this: > > First, I have a self-referential table, named "users" > create_table :users do |t| > t.column :name, :string > t.column :user_name, :string > t.column :password, :string > t.column :email, :string > end > > I also have a HABTM join table, named "follow_relations" > create_table :follow_realtions do |t| > t.column :a_id, :integer # a_id follows b_id > t.column :b_id, :integer > end > > My HABTM join is defined as follows > has_and_belongs_to_many :followers, > :class_name => > "User", > :join_table => > "follow_relations", > :foreign_key => > "b_id", > :association_foreign_key > => "a_id" > > has_and_belongs_to_many :followings, > :class_name => "User", > :join_table => > "follow_relations", > :foreign_key => > "a_id", > :association_foreign_key > => "b_id"[...]> The first << operation executes the following SQL : > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 1, 3) > The second << operation executes the following SQL: > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3) > Thus, key id is duplicated. It looks like that every time, the value > of column id is copied from column a_id. > What''s going on here? How to fix this problem?If you''re using HABTM, the join table (follow_relations) should not have an id column. Remove it and see if that does the trick.> > Thanks in advance, > xiahongBest, -- Marnen Laibow-Koser http://www.marnen.org marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org -- Posted via http://www.ruby-forum.com/.
If I remove id column, what is the primary key? 2009/7/3 Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>> > Xiahong Gao wrote: > > Hi everyone, > > > > I ran into a serious problem when I was trying to do something similar > > to twitter follow mechanism. The thing is like this: > > > > First, I have a self-referential table, named "users" > > create_table :users do |t| > > t.column :name, :string > > t.column :user_name, :string > > t.column :password, :string > > t.column :email, :string > > end > > > > I also have a HABTM join table, named "follow_relations" > > create_table :follow_realtions do |t| > > t.column :a_id, :integer # a_id follows b_id > > t.column :b_id, :integer > > end > > > > My HABTM join is defined as follows > > has_and_belongs_to_many :followers, > > :class_name => > > "User", > > :join_table => > > "follow_relations", > > :foreign_key => > > "b_id", > > :association_foreign_key > > => "a_id" > > > > has_and_belongs_to_many :followings, > > :class_name => "User", > > :join_table => > > "follow_relations", > > :foreign_key => > > "a_id", > > :association_foreign_key > > => "b_id" > [...] > > The first << operation executes the following SQL : > > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 1, 3) > > The second << operation executes the following SQL: > > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3) > > Thus, key id is duplicated. It looks like that every time, the value > > of column id is copied from column a_id. > > What''s going on here? How to fix this problem? > > If you''re using HABTM, the join table (follow_relations) should not have > an id column. Remove it and see if that does the trick. > > > > > Thanks in advance, > > xiahong > > Best, > -- > Marnen Laibow-Koser > http://www.marnen.org > marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > -- > Posted via http://www.ruby-forum.com/. > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
2009/7/3 gaoxh gaoxh04 <gaoxh04-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> If I remove id column, what is the primary key?Have a look at the rails guides, particularly the one on ActiveRecord associations at http://guides.rubyonrails.org/index.html Colin> > 2009/7/3 Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> >> >> Xiahong Gao wrote: >> > Hi everyone, >> > >> > I ran into a serious problem when I was trying to do something similar >> > to twitter follow mechanism. The thing is like this: >> > >> > First, I have a self-referential table, named "users" >> > create_table :users do |t| >> > t.column :name, :string >> > t.column :user_name, :string >> > t.column :password, :string >> > t.column :email, :string >> > end >> > >> > I also have a HABTM join table, named "follow_relations" >> > create_table :follow_realtions do |t| >> > t.column :a_id, :integer # a_id follows b_id >> > t.column :b_id, :integer >> > end >> > >> > My HABTM join is defined as follows >> > has_and_belongs_to_many :followers, >> > :class_name => >> > "User", >> > :join_table => >> > "follow_relations", >> > :foreign_key => >> > "b_id", >> > >> > :association_foreign_key >> > => "a_id" >> > >> > has_and_belongs_to_many :followings, >> > :class_name => "User", >> > :join_table => >> > "follow_relations", >> > :foreign_key => >> > "a_id", >> > :association_foreign_key >> > => "b_id" >> [...] >> > The first << operation executes the following SQL : >> > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 1, 3) >> > The second << operation executes the following SQL: >> > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3) >> > Thus, key id is duplicated. It looks like that every time, the value >> > of column id is copied from column a_id. >> > What''s going on here? How to fix this problem? >> >> If you''re using HABTM, the join table (follow_relations) should not have >> an id column. Remove it and see if that does the trick. >> >> > >> > Thanks in advance, >> > xiahong >> >> Best, >> -- >> Marnen Laibow-Koser >> http://www.marnen.org >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org >> -- >> Posted via http://www.ruby-forum.com/. >> >> >
thanks all... problem solved. 2009/7/3 Colin Law <clanlaw-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org>> > 2009/7/3 gaoxh gaoxh04 <gaoxh04-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > If I remove id column, what is the primary key? > > Have a look at the rails guides, particularly the one on ActiveRecord > associations at http://guides.rubyonrails.org/index.html > > Colin > > > > > 2009/7/3 Marnen Laibow-Koser <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> > >> > >> Xiahong Gao wrote: > >> > Hi everyone, > >> > > >> > I ran into a serious problem when I was trying to do something similar > >> > to twitter follow mechanism. The thing is like this: > >> > > >> > First, I have a self-referential table, named "users" > >> > create_table :users do |t| > >> > t.column :name, :string > >> > t.column :user_name, :string > >> > t.column :password, :string > >> > t.column :email, :string > >> > end > >> > > >> > I also have a HABTM join table, named "follow_relations" > >> > create_table :follow_realtions do |t| > >> > t.column :a_id, :integer # a_id follows b_id > >> > t.column :b_id, :integer > >> > end > >> > > >> > My HABTM join is defined as follows > >> > has_and_belongs_to_many :followers, > >> > :class_name => > >> > "User", > >> > :join_table => > >> > "follow_relations", > >> > :foreign_key => > >> > "b_id", > >> > > >> > :association_foreign_key > >> > => "a_id" > >> > > >> > has_and_belongs_to_many :followings, > >> > :class_name => "User", > >> > :join_table => > >> > "follow_relations", > >> > :foreign_key => > >> > "a_id", > >> > > :association_foreign_key > >> > => "b_id" > >> [...] > >> > The first << operation executes the following SQL : > >> > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 1, 3) > >> > The second << operation executes the following SQL: > >> > INSERT INTO `follow_relations (`id`, `b_id`, `a_id`) VALUES (3, 2, 3) > >> > Thus, key id is duplicated. It looks like that every time, the value > >> > of column id is copied from column a_id. > >> > What''s going on here? How to fix this problem? > >> > >> If you''re using HABTM, the join table (follow_relations) should not have > >> an id column. Remove it and see if that does the trick. > >> > >> > > >> > Thanks in advance, > >> > xiahong > >> > >> Best, > >> -- > >> Marnen Laibow-Koser > >> http://www.marnen.org > >> marnen-sbuyVjPbboAdnm+yROfE0A@public.gmane.org > >> -- > >> Posted via http://www.ruby-forum.com/. > >> >> > > > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---