# Exception when destroying an object with :through association

**URL:** <https://www.ruby-forum.com/t/exception-when-destroying-an-object-with-through-association/121175>\
**Category:** Rails\
**Created:** [November 24, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/exception-when-destroying-an-object-with-through-association/121175 "2007-11-24T22:23:24Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [November 24, 2007, 10:23pm UTC](https://www.ruby-forum.com/t/exception-when-destroying-an-object-with-through-association/121175/1 "2007-11-24T22:23:24Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [November 24, 2007, 11:16pm UTC](https://www.ruby-forum.com/t/exception-when-destroying-an-object-with-through-association/121175/2 "2007-11-24T23:16:39Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![stan.baptista](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ccd318/32.png) [@stan.baptista](https://www.ruby-forum.com/u/stan.baptista)\
**Post date:** [November 24, 2007, 11:24pm UTC](https://www.ruby-forum.com/t/exception-when-destroying-an-object-with-through-association/121175/3 "2007-11-24T23:24:52Z")

</div>

> 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.
