简体   繁体   中英

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.

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 id column or you need define your relation between genre and song like an has_many_and_belongs_to relation.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM