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

On 24 Nov 2007, at 21:22, stan.baptista wrote:

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

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.