Rails:在已经使用 UUID 作为主键的数据库中将 UUID 主键添加到 Table

Rails: Add UUID Primary Key to Table in Database Already Using UUIDs as Primary Keys

在 Rails 5.2 中,我有一个 has_and_belongs_to_many join table,我正在将其转换为 has_many :through 关系,因此现在需要一个主键table.

我已经在我的 Postgres 数据库中使用 UUID 作为主键,因此我需要添加一个 UUID 主键列,但语法不正确。

我试过了...

add_column :products_uw_questions, :uuid, :primary_key

但这只是创建了一个基于整数的列。

我也试过了...

add_column :products_uw_questions, :id, :primary_key

认为已经存在的 UUID 设置会解决它,但运气不好。

如果我的数据库已经在使用 UUID 主键,正确的语法是什么?

不断尝试直到成功...

def change
  add_column :products_questions, :id, :uuid, primary_key: true, default: -> { "gen_random_uuid()" }
end