Skip to content

Instantly share code, notes, and snippets.

@AlessandroLorenzi
Created August 6, 2022 23:15
Show Gist options
  • Save AlessandroLorenzi/98ce9b783cf3946251c81c77cf2c8e57 to your computer and use it in GitHub Desktop.
Save AlessandroLorenzi/98ce9b783cf3946251c81c77cf2c8e57 to your computer and use it in GitHub Desktop.
Testing json generated columns
DROP TABLE activities;
CREATE TABLE activities (
id serial NOT NULL PRIMARY KEY,
activity json NOT NULL,
type text GENERATED ALWAYS AS (activity ->> 'type') STORED,
name text GENERATED ALWAYS AS (activity ->> 'name') STORED
);
INSERT INTO
activities (activity)
VALUES
(
'{
"type": "Foo",
"name": "Bar",
"quantity": 1
}'
);
INSERT INTO
activities (activity)
VALUES
(
'{
"type": "Hello",
"name": "World",
"quantity": 2
}'
);
SELECT
type,
name,
activity ->> 'quantity' AS quantity
FROM
activities;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment