ruby-on-rails – Activerecord迁移,使t.references正确指向自定
发布时间:2020-12-17 04:02:54 所属栏目:百科 来源:网络整理
导读:下面我有一个“test”模型的迁移,它使用它自己的主键,一个String而不是一个Integer. class CreateTest ActiveRecord::Migration[5.1] def change create_table :test,id: false do |t| t.string :id,primary_key: true t.timestamps end end end 现在我们有
下面我有一个“test”模型的迁移,它使用它自己的主键,一个String而不是一个Integer.
class CreateTest < ActiveRecord::Migration[5.1] def change create_table :test,id: false do |t| t.string :id,primary_key: true t.timestamps end end end 现在我们有了“客户”模型进行t.references测试. class CreateClients < ActiveRecord::Migration[5.1] def change create_table :clients do |t| t.references :test,null: false t.timestamps end end end 问题是t.references假设它是一个整数id. # == Schema Information # # Table name: clients # # id :integer not null,primary key # test_id :integer not null # created_at :datetime not null # updated_at :datetime not null 这显然是错误的,因为Test.id是一个字符串. 我是否需要做一些魔术让t.references“知道”它是基于模型的字符串或什么? 谢谢. 解决方法
使用引用将其添加到迁移中:
type: :string
You can read more here. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |