【发布时间】:2016-06-29 22:13:24
【问题描述】:
尝试更新 Postgres ENUM 列的值会引发以下异常:
ActiveRecord::StatementInvalid Exception: PG::DatatypeMismatch: ERROR: column "interesting_column" 的类型是interesting_thing,但表达式的类型是整数
第 1 行:UPDATE "interesting_table" SET "interesting_column" = 0, "updated_a...
提示:您需要重写或强制转换表达式。
InterestingTable.first.update_attributes!(normal_column: 'food')
# => perfectly fine
InterestingTable.first.update_attributes!(interesting_column: 'foo')
# => above exception
这里是创建表的迁移:
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
【问题讨论】:
标签: ruby-on-rails postgresql ruby-on-rails-3 ruby-on-rails-4 enums