我使用以下命令创建了模型:
rails g model UserCertification user:references certification:references certification_no:string
这是指我的设计用户模型user:references
。
在db:migrate上,我收到以下错误:造成原因:
ActiveRecord :: MismatchedForeignKey:表格上的列
user_id
user_certifications
与id
上的users
列不匹配,输入bigint(20)
。要解决此问题,请更改user_id
上的user_certifications
列为:bigint。 (例如t.bigint :user_id
)。原始消息:Mysql2 ::错误:无法添加外键约束
这是我的移民
class CreateUserCertifications < ActiveRecord::Migration[6.0]
def change
create_table :user_certifications do |t|
t.references :user, null: false, foreign_key: true
t.references :certification, null: false, foreign_key: true
t.string :certification_no
t.timestamps
end
end
end
我尝试使用t.references :user, null: false, foreign_key: true,type: :integer
将类型设置为整数
我还删除了模型并重做并与以前的参考进行了检查,但均未成功。也许有人有主意?
尝试在引用中设置类型以匹配其他表:
t.references(:user, null: false, type: :bigint)
如果不起作用,请尝试在user_certifications中显式创建外键:
t.bigint :user_id, null: false
t.references(:user)