【发布时间】:2021-01-20 00:21:09
【问题描述】:
我使用 uuid 而不是 bigint 作为主键。以及下一次迁移
class CreateProjects < ActiveRecord::Migration[6.0]
def change
create_table :projects, id: :uuid do |t|
t.string :title
t.references :user, null: false, foreign_key: true
t.timestamps
end
end
end
因错误而失败:
PG::DatatypeMismatch: ERROR: foreign key constraint "fk_rails_b872a6760a" cannot be implemented
DETAIL: Key columns "user_id" and "id" are of incompatible types: bigint and uuid.
【问题讨论】:
-
@Salil 特别是rafaecheve's comment 暗示
t.belongs_to ... type: :uuid(PS:t.belongs_to是t.references的别名)。
标签: ruby-on-rails postgresql database-migration uuid