PostgreSQL: CHECK constraint for non-empty JSON objects
2 answers
Just like any validation constraint and use of <> operators. From the manual:
The standard comparison operators shown in Table 9-1 are available for jsonb, but not for json.
And table 9.1 shows the operator not equal to <>:
create table foo(
bar jsonb,
constraint baz check(bar <> '{}'::jsonb)
);
insert into foo(bar) values('{"foo": 1}'::jsonb);
insert into foo(bar) values('{}'::jsonb); -- fails
+1
source to share