Created
August 6, 2022 23:15
-
-
Save AlessandroLorenzi/98ce9b783cf3946251c81c77cf2c8e57 to your computer and use it in GitHub Desktop.
Testing json generated columns
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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