stan.baptista
2007-Nov-24 21:22 UTC
Exception when destroying an object with :through association
I have an association such that a Person has many Events :through
Attendees. When I attempt to destroy a Person object, I get the
following exception:
ActiveRecord::StatementInvalid in PeopleController#destroy
Mysql::Error: #42S22Unknown column ''id'' in ''where
clause'':
DELETE FROM attendees
WHERE `id` = NULL
There is no column `id` by design so it''s not surprising that I get
the error. Why is Rails looking for an id?
SCHEMA:
ActiveRecord::Schema.define(:version => 3) do
create_table "attendees", :id => false, :force => true do |t|
t.column "event_id", :integer
t.column "person_id", :integer
t.column "bit_flags", :integer
end
add_index "attendees", ["event_id"], :name =>
"index_attendees_on_event_id"
add_index "attendees", ["person_id"], :name =>
"index_attendees_on_person_id"
create_table "events", :force => true do |t|
t.column "name", :string
t.column "description", :text
t.column "event_date", :date
t.column "start_time", :time
t.column "end_time", :time
t.column "last_register_date", :date
t.column "location_city", :string
t.column "location_state", :string, :limit => 2
t.column "location_zip", :string, :limit => 9
t.column "location_addr", :text
t.column "agenda", :text
t.column "contacts", :text
end
create_table "people", :force => true do |t|
t.column "first_name", :string
t.column "last_name", :string
t.column "title", :string
t.column "organization", :string
t.column "email", :string
t.column "phone1", :string
t.column "phone2", :string
end
end
MODELS:
class Person < ActiveRecord::Base
has_many :attendees, :dependent => :destroy
has_many :events, :through => :attendees, :uniq => true
end
class Event < ActiveRecord::Base
has_many :attendees, :dependent => :destroy
has_many :people, :through => :attendees, :uniq => true
end
class Attendee < ActiveRecord::Base
belongs_to :event
belongs_to :person
end
Any ideas? Thanks,
Stan
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2007-Nov-24 22:15 UTC
Re: Exception when destroying an object with :through association
On 24 Nov 2007, at 21:22, stan.baptista wrote:> > I have an association such that a Person has many Events :through > Attendees. When I attempt to destroy a Person object, I get the > following exception: > > ActiveRecord::StatementInvalid in PeopleController#destroy > Mysql::Error: #42S22Unknown column ''id'' in ''where clause'': > DELETE FROM attendees > WHERE `id` = NULL > > There is no column `id` by design so it''s not surprising that I get > the error. Why is Rails looking for an id? >Because rails expects every table to have a primary key (the exception was the old habtm join tables). With has_many :through, the join model is a model in its own right so rails expects it to have a primary key. Fred> SCHEMA: > > ActiveRecord::Schema.define(:version => 3) do > > create_table "attendees", :id => false, :force => true do |t| > t.column "event_id", :integer > t.column "person_id", :integer > t.column "bit_flags", :integer > end > > add_index "attendees", ["event_id"], :name => > "index_attendees_on_event_id" > add_index "attendees", ["person_id"], :name => > "index_attendees_on_person_id" > > create_table "events", :force => true do |t| > t.column "name", :string > t.column "description", :text > t.column "event_date", :date > t.column "start_time", :time > t.column "end_time", :time > t.column "last_register_date", :date > t.column "location_city", :string > t.column "location_state", :string, :limit => 2 > t.column "location_zip", :string, :limit => 9 > t.column "location_addr", :text > t.column "agenda", :text > t.column "contacts", :text > end > > create_table "people", :force => true do |t| > t.column "first_name", :string > t.column "last_name", :string > t.column "title", :string > t.column "organization", :string > t.column "email", :string > t.column "phone1", :string > t.column "phone2", :string > end > > end > > MODELS: > > class Person < ActiveRecord::Base > has_many :attendees, :dependent => :destroy > has_many :events, :through => :attendees, :uniq => true > end > > class Event < ActiveRecord::Base > has_many :attendees, :dependent => :destroy > has_many :people, :through => :attendees, :uniq => true > end > > class Attendee < ActiveRecord::Base > belongs_to :event > belongs_to :person > end > > Any ideas? Thanks, > Stan > > >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---
stan.baptista
2007-Nov-24 22:24 UTC
Re: Exception when destroying an object with :through association
> With has_many :through, the join model is a model in its own right so rails expects it to have a primary key.Ah. Thanks. --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---