Q:

Setting default value for a json column

I am looking at using Postgres's feature of setting json into a column via activerecords json handling features I am wondering how I would give it a default value upon table creation of something like {name: '', other_name: ''} and so on ...
I am also looking to understand how if I create a default json value for a column, like the example above and then later on I fill in the values, but then at a some other time reset it back to "default" how that would look.

A:

It's just like any other default, once you fix up the json syntax:
CREATE TABLE mytable (
    someothercol integer,
    somecol json DEFAULT '{"name": "", "other_name": ""}'
);

If you set to DEFAULT, it does just that:
regress=> INSERT INTO mytable(someothercol, somecol) VALUES (42, '{"nondefault": 1}');
INSERT 0 1
regress=> SELECT * FROM mytable;
 someothercol |      somecol      
--------------+-------------------
           42 | {"nondefault": 1}
(1 row)

regress=> UPDATE mytable SET somecol = DEFAULT WHERE someothercol = 42;
UPDATE 1
regress=> SELECT * FROM mytable;
 someothercol |            somecol             
--------------+--------------------------------
           42 | {"name": "", "other_name": ""}
(1 row)

