【发布时间】:2018-02-19 01:52:28
【问题描述】:
我一直在尝试将我的 Postgres 数据库中的列类型从文本更改为 json,但没有成功。这是我尝试过的...
class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0]
def up
execute 'ALTER TABLE places ALTER COLUMN notes TYPE json USING (notes::json)'
end
def down
execute 'ALTER TABLE places ALTER COLUMN notes TYPE text USING (notes::text)'
end
end
还有……
class ChangeNotesTypeInPlaces < ActiveRecord::Migration[5.0]
def up
change_column :places, :notes, 'json USING CAST(notes AS json)'
end
def down
change_column :places, :notes, 'text USING CAST(notes AS text)'
end
end
这两个都返回相同的错误...
PG::InvalidTextRepresentation: ERROR: invalid input syntax for type json
【问题讨论】:
-
notes列是否已经有一个不是有效 json 的值? -
这是个好问题。我从来没有在这个列中保存过数据(在开发中,不幸的是在生产中不是同样的情况),但我也没有对该列的非空约束。可能是它一直在将一个空字符串保存到导致这种情况的列中吗?如果是,你知道我该如何解决这个问题吗?
-
如果你没有非空约束,那么空值就可以了,或者你可以保存空 json
{}。它不接受空字符串。
标签: ruby-on-rails postgresql rails-migrations