下面我有一个“测试”模型的迁移,它使用它自己的主键,一个字符串而不是一个整数。
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
“知道”它是一个基于模型或其他东西的字符串吗?
谢谢。
最佳答案
使用 references
将其添加到迁移中:
type: :string
The reference column type. Defaults to :integer.
关于ruby-on-rails - Activerecord 迁移,使 t.references 正确指向自定义 ID 类型,字符串而不是整数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49430757/