ruby on rails - Foreign key does not exist when deleting dependent records -
i have 2 models. first one:
class keyword < activerecord::base has_many :words, dependent: :delete_all end
the second:
class word < activerecord::base end
my migrations:
class keywords < activerecord::migration def change create_table :keywords |t| t.string :name, null: false, unique: true t.string :description, null: false t.string :keys, null: false t.timestamps null: false end end end
and words:
class words < activerecord::migration def change create_table :words |t| t.belongs_to :keyword t.string :name, null: false t.timestamps null: false end end end
when i'm trying delete keywords instance rails throws out following exception:
pg::undefinedcolumn: error: column words.keyword_id not exist line 1: delete "words" "words"."keyword_id" = $1 ^ : delete "words" "words"."keyword_id" = $1
so question is, why rails created keyword_id
reference table instead of using keywords_id
? , how fix it.
the code in repository different migration code posted. in repo have:
t.belongs_to :keywords
change to:
t.belongs_to :keyword
as posted in question , recreate database.
Comments
Post a Comment