djburdick
djburdick

Reputation: 11940

rails :dependent => :destroy error - want to use alternate id for deletion

I've got a model named Song with

has_many :genre_songs, :dependent => :destroy

I've got a join model GenreSong (with genre_id and song_id) that I want to destroy when the Song records are deleted.

It appears that rails is looking for a primary key to delete in the GenreSong model, since I get this error:

 Mysql::Error: Unknown column 'id' in 'where clause': DELETE FROM `genre_songs` WHERE `id` = NULL

Is there another way to do this (I would think deleting on song_id would be enough)? By specifying what id the delete should be performed on.

Maybe my SQL is off, but it seems wrong to me to have an extra primary key in a joins table that should consist only of two other primary keys.

Upvotes: 0

Views: 725

Answers (1)

shingara
shingara

Reputation: 46914

It's because your join table is more like an has_many_and_belongs_to

It's the only case where you don't need the id column in your table. In all other case, you need define your primary-key. By default it's id.

So you need have this idcolumn or you need define your relation between genre and song like an has_many_and_belongs_to relation.

Upvotes: 3

Related Questions