我一直在尝试将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的值了吗? - jamesjaya{}
。它不接受空字符串。 - jamesjaya