开发者

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 record开发者_C百科s 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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜