将 postgres ENUM 与 rails 一起使用会产生 PG::DatatypeMismatch
Using postgres ENUM with rails yields `PG::DatatypeMismatch`
尝试更新 Postgres ENUM 列的值引发以下异常:
ActiveRecord::StatementInvalid Exception: PG::DatatypeMismatch: ERROR: column "interesting_column" is of type interesting_thing but expression is of type integer
LINE 1: UPDATE "interesting_table" SET "interesting_column" = 0, "updated_a...
HINT: You will need to rewrite or cast the expression.
InterestingTable.first.update_attributes!(normal_column: 'food')
# => perfectly fine
InterestingTable.first.update_attributes!(interesting_column: 'foo')
# => above exception
这是创建 table 的迁移:
class CreateInterestingTables < ActiveRecord::Migration
def up
execute <<-SQL
CREATE TYPE normal_thing AS ENUM ('food', 'water', 'shelter');
CREATE TYPE interesting_thing AS ENUM ('foo', 'bar', 'baz');
SQL
create_table :interesting_tables do |t|
t.column :normal_column, :normal_thing
t.column :interesting_column, :interesting_thing
end
end
def down
drop_table :interesting_tables
execute 'DROP TYPE normal_thing'
execute 'DROP TYPE interesting_thing'
end
end
问题在于,虽然该列在数据库中具有正确的类型,但活动记录会尝试将其解释为 integer
。您可以通过 运行:
验证
InterestingTable.columns
# => [#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f7567f82260
# @coder=nil,
# @default=nil,
# @limit=nil,
# @name="id",
# @null=false,
# @precision=nil,
# @primary=true,
# @scale=nil,
# @sql_type="integer",
# @type=:integer>,
# #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f7568075690
# @coder=nil,
# @default=nil,
# @limit=nil,
# @name="normal_column",
# @null=true,
# @precision=nil,
# @primary=false,
# @scale=nil,
# @sql_type="normal_thing",
# @type=nil>,
# #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f7568075668
# @coder=nil,
# @default=nil,
# @limit=nil,
# @name="interesting_column",
# @null=true,
# @precision=nil,
# @primary=false,
# @scale=nil,
# @sql_type="interesting_thing",
# @type=:integer>]
请注意第二列的类型是 nil
,而最后一列的类型是 integer
。 String#to_i
returns 0
如果您的字符串不是以数字开头,因此您会收到您尝试分配 0
.
的错误
但这是为什么呢?原因 - interesting_thing
包含子字符串 int
,适配器将其视为 integer
。这似乎是一个长期存在的错误,直到 rails 4.2 才得到修复。 The offending method.
可能的解决方案:
- 迁移到 rails 4.2+
- 将您的 ENUM 重命名为不匹配的名称
- 猴子补丁适配器。这里有一个quick fix.
尝试更新 Postgres ENUM 列的值引发以下异常:
ActiveRecord::StatementInvalid Exception: PG::DatatypeMismatch: ERROR: column "interesting_column" is of type interesting_thing but expression is of type integer
LINE 1: UPDATE "interesting_table" SET "interesting_column" = 0, "updated_a...
HINT: You will need to rewrite or cast the expression.
InterestingTable.first.update_attributes!(normal_column: 'food')
# => perfectly fine
InterestingTable.first.update_attributes!(interesting_column: 'foo')
# => above exception
这是创建 table 的迁移:
class CreateInterestingTables < ActiveRecord::Migration
def up
execute <<-SQL
CREATE TYPE normal_thing AS ENUM ('food', 'water', 'shelter');
CREATE TYPE interesting_thing AS ENUM ('foo', 'bar', 'baz');
SQL
create_table :interesting_tables do |t|
t.column :normal_column, :normal_thing
t.column :interesting_column, :interesting_thing
end
end
def down
drop_table :interesting_tables
execute 'DROP TYPE normal_thing'
execute 'DROP TYPE interesting_thing'
end
end
问题在于,虽然该列在数据库中具有正确的类型,但活动记录会尝试将其解释为 integer
。您可以通过 运行:
InterestingTable.columns
# => [#<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f7567f82260
# @coder=nil,
# @default=nil,
# @limit=nil,
# @name="id",
# @null=false,
# @precision=nil,
# @primary=true,
# @scale=nil,
# @sql_type="integer",
# @type=:integer>,
# #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f7568075690
# @coder=nil,
# @default=nil,
# @limit=nil,
# @name="normal_column",
# @null=true,
# @precision=nil,
# @primary=false,
# @scale=nil,
# @sql_type="normal_thing",
# @type=nil>,
# #<ActiveRecord::ConnectionAdapters::PostgreSQLColumn:0x007f7568075668
# @coder=nil,
# @default=nil,
# @limit=nil,
# @name="interesting_column",
# @null=true,
# @precision=nil,
# @primary=false,
# @scale=nil,
# @sql_type="interesting_thing",
# @type=:integer>]
请注意第二列的类型是 nil
,而最后一列的类型是 integer
。 String#to_i
returns 0
如果您的字符串不是以数字开头,因此您会收到您尝试分配 0
.
但这是为什么呢?原因 - interesting_thing
包含子字符串 int
,适配器将其视为 integer
。这似乎是一个长期存在的错误,直到 rails 4.2 才得到修复。 The offending method.
可能的解决方案:
- 迁移到 rails 4.2+
- 将您的 ENUM 重命名为不匹配的名称
- 猴子补丁适配器。这里有一个quick fix.