PostgreSQL update table then insert on another table
1
vote
1
answer
3155
views
I have a
User
table and a Log
table. I need to update a field on the User
table and insert a new entry on the Log
table recording the update. I have written the statement for the update:
UPDATE users SET free_shipping_until = spu.created_at::date + interval '1 year'
FROM shipping_priority_users AS spu
WHERE spu.role_id = #{role.id} and users.id = spu.user_id and spu.created_at is not null;
For each update I need to also add (probably in a transaction) an insert statement on the Log
table which has the following columns
user_id: string,
created_at: timestamp
data: jsonb
- The data column contains a jsonb value including the free_shipping_until
value from the update.
data: {"free_shipping_until": [null, "2021-07-30"]}
- user_id
should match the updated record's value
- The created_at
column is the current time, I'm using RoR and could interpolate the value with the expected format.
I'm using PostgreSQL 12.3
Asked by vicocas
(125 rep)
Sep 21, 2020, 08:38 AM
Last activity: Sep 21, 2020, 09:14 AM
Last activity: Sep 21, 2020, 09:14 AM