简体   繁体   English

如何在rails中使用级联删除将列添加为外键

[英]How to add column as foreign key with cascade delete in rails

I have two table one product and another usage我有两个表一个产品和另一个用途

Product
#  id                  :integer          not null, primary key
#  product_name        :string           not null
#  plan_id             :string
#  plan_name           :string

Usage
#  id              :integer          not null, primary key
#  quantity        :float
#  date            :date

Want to add product_id as foreign key in usage想在使用中添加 product_id 作为外键

i am trying to run migration我正在尝试运行迁移

 def change
    add_reference(:usages, :products, foreign_key: { on_delete: :cascade })
 end

Getting error column "product_id" referenced in foreign key constraint does not exist获取外键约束中引用的错误列“product_id”不存在

解决方案是 ref_name 应该是单数

 add_reference(:usages, :product, foreign_key: { on_delete: :cascade })

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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