So I'm starting to use the Postgres JSON datatype, now that there's a lot of fun stuff you can do with it. In one of my Rails apps which is not yet Rails 4 (where support for Postgres JSON has been added) I added a JSON column like this:
create_table :foo do |t|
t.column :bar, :json
end
but I can't figure out how to set a default value for the column.
I tried all variations like {}
, '{}'
, '{}'::json
, '[]'::json
etc. but I either get an error when the migration runs or it simply doesn't work, meaning the migration runs but, when I create a new Foo
, bar
is nil
.
after_initialize
callback but I generally don't like those... – Mendive""
, I believe that is what is used for hstore to denote an empty hash – Bukavu-- add_column(:foo, :bar, :json, {:default=>""}) rake aborted! An error has occurred, this and all later migrations canceled: PG::InvalidTextRepresentation: ERROR: invalid input syntax for type json DETAIL: The input string ended unexpectedly. CONTEXT: JSON data, line 1: : ALTER TABLE "foo" ADD COLUMN "bar" json DEFAULT ''
– Mendivenull
by default... by default. I don't want it to benull
, I want it to be{}
. – Mendive