[英]ActiveRecord::Enum - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer:
I am having a strange error and was hoping someone could point me in the right direction.我有一个奇怪的错误,希望有人能指出我正确的方向。 I have a model called Organizations, and a attribute called department
, see the excerpt from the schema below:我有一个名为 Organizations 的模型和一个名为department
的属性,请参阅以下架构的摘录:
t.integer "department", default: 0
Inside my model have defined my enums for this attribute, as I am using ActiveRecord::Enum , like below:在我的模型中为这个属性定义了我的枚举,因为我使用的是ActiveRecord::Enum ,如下所示:
enum department: [:conferences, :design_teams, :services, :clubs, :events, :communications]
But when I query, JobPosting.joins(job: :organization).where(organizations: { department: 'conferences' })
I get an error that reads:但是当我查询时, JobPosting.joins(job: :organization).where(organizations: { department: 'conferences' })
我收到一个错误消息:
PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: "conferences"
FYI: An Organization has_many Jobs, and Job has_many JobPostings.仅供参考:一个组织有_many Jobs,而Job has_many JobPostings。
But when I query Organization.where(department: 'conferences')
it works.但是当我查询Organization.where(department: 'conferences')
它工作。
Any help would be greatly appreciated.任何帮助将不胜感激。
Other way is to set text-based enums.另一种方法是设置基于文本的枚举。 In my opinion it is the best way for enum:在我看来,这是枚举的最佳方式:
DEPARTMENTS_ENUM = [:conferences, :design_teams, :services, :clubs, :events, :communications]
enum department: Hash[*DEPARTMENTS_ENUM.collect{|v| [v, v]}.flatten()]
It will work after department column type will change.它会在部门列类型更改后起作用。
Organization.where(department: 'conferences')
Will work too也会工作
I am not directly answering the question but as it is the first link on google when we search PG::InvalidTextRepresentation: ERROR: invalid input value for enum
我没有直接回答这个问题,但是当我们搜索PG::InvalidTextRepresentation: ERROR: invalid input value for enum
时,它是 google 上的第一个链接
it can help:它可以帮助:
it is possible to declare our enum like this, this way it will not convert the string to an integer but let this job to postgresql.可以像这样声明我们的枚举,这样它就不会将字符串转换为整数,而是让这个工作交给 postgresql。
enum provider: { ig: "ig", fb: "fb", powertrack: "powertrack", gnip: "gnip" }
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.