Ruby on rails 主键和外键超出范围

Ruby on rails 主键和外键超出范围,ruby-on-rails,postgresql,foreign-keys,rails-activerecord,Ruby On Rails,Postgresql,Foreign Keys,Rails Activerecord,我有一个人模型: id: int, uuid:bigint 以及一篇博文: id: int, uuid:bigint, person_id: int 我希望blogpost.person返回人物模型的uuid,而不是他们的id 我尝试了个人类: has_many :blogposts, dependent: :delete_all, inverse_of: :deck, primary_key: :uuid 但我得到: ActiveModel::RangeError: 1534420711

我有一个人模型:

id: int, uuid:bigint
以及一篇博文:

id: int, uuid:bigint, person_id: int
我希望blogpost.person返回人物模型的uuid,而不是他们的id

我尝试了个人类:

has_many :blogposts, dependent: :delete_all, inverse_of: :deck, primary_key: :uuid
但我得到:

ActiveModel::RangeError: 1534420711008 is out of range for ActiveModel::Type::Integer with limit 4 bytes.

任何帮助都将不胜感激。

您已经将外键person\u id定义为int,但您正试图让它将关联的bigint uuid存储在int字段中。请尝试在您的模式中对person_id使用bigint

这是假设您已将uuid设置为Person的主键。如果您没有:

编辑

正如您所说的,您不想让uuid成为person的主键。外键通常引用另一个表中的主键,但也可以引用唯一的索引列

首先,按照惯例,blogpost.person不应该返回uuid,而应该返回person对象。因此,简单的方法是添加范围或方法:

class Blogpost < ApplicationModel

  def person
    Person.find_by_uuid(person_uuid)
  end

end

但是,如果person_uuid和uuid不是主键,请确保在迁移中将它们定义为唯一索引。唯一性:在列定义中为true

可能存在一点点重复谢谢您的努力,但我不希望person的主键是它的uuid。我更希望blog对象上的person_id字段只引用person上的uuid字段。这不可能吗?
class Blogpost < ApplicationModel
  belongs_to :person, foreign_key: :person_uuid
end

class Person < ApplicationModel
  has_many :blogposts, foreign_key: :person_uuid, inverse_of: :person, primary_key: :uuid
end