简体   繁体   English

ActiveRecord::Enum - PG::InvalidTextRepresentation:错误:整数输入语法无效:

[英]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.任何帮助将不胜感激。

This works on ror5.这适用于 ror5。

JobPosting.joins(job: :organization).where(organizations: 
{ department: Organization.departments['conferences'] }) 

I'm not even sure if enum was available in ror3.我什至不确定enum在 ror3 中是否可用

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.

相关问题 PG :: InvalidTextRepresentation:错误:整数的无效输入语法 - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer Friendly_id PG :: InvalidTextRepresentation:错误:整数的无效输入语法: - Friendly_id PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: PG :: InvalidTextRepresentation:错误:整数的无效输入语法:“ aaa” - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “aaa” heroku:PG :: InvalidTextRepresentation:错误:整数的无效输入语法:“” - heroku: PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “” PG :: InvalidTextRepresentation:ERROR:整数的输入语法无效:“M” - PG::InvalidTextRepresentation: ERROR: invalid input syntax for integer: “M” InvalidTextRepresentation:错误:整数的无效输入语法:“ all” - InvalidTextRepresentation: ERROR: invalid input syntax for integer: “all” ActiveRecord :: StatementInvalid(PG :: InvalidDatetimeFormat:错误:类型为date的输入语法无效:“” - ActiveRecord::StatementInvalid (PG::InvalidDatetimeFormat: ERROR: invalid input syntax for type date: “” Rails迁移 - PG ::错误:错误:整数的输入语法无效:“” - Rails Migration - PG::Error: ERROR: invalid input syntax for integer: “” PG错误:尝试销毁时整数的无效输入语法 - PG ERROR: invalid input syntax for integer when trying to destroy Ruby on Rails测试-ActiveRecord :: StatementInvalid:PG :: InvalidTextRepresentation:错误:格式不正确的数组文字: - Ruby on Rails Testing - ActiveRecord::StatementInvalid: PG::InvalidTextRepresentation: ERROR: malformed array literal:
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM