I have table as following:
dev=> \d statemachine_history
Table "public.statemachine_history"
Column | Type | Modifiers
---------------+--------------------------+-------------------------------------------------------------------
id | bigint | not null default nextval('statemachine_history_id_seq'::regclass)
schema_name | character varying | not null
event | character varying | not null
identifier | integer | not null
initial_state | character varying | not null
final_state | character varying | not null
triggered_at | timestamp with time zone | not null default statement_timestamp()
triggered_by | text |
command | json |
flag | json |
created_at | timestamp with time zone |
created_by | json |
updated_at | timestamp with time zone |
updated_by | json |
Indexes:
"statemachine_log_pkey" PRIMARY KEY, btree (id)
"unique_statemachine_log_id" UNIQUE, btree (id)
"statemachine_history_identifier_idx" btree (identifier)
"statemachine_history_schema_name_idx" btree (schema_name)
AND
dev=> \d booking
Table "public.booking"
Column | Type | Modifiers
----------------+--------------------------+------------------------------------------------------
id | bigint | not null default nextval('booking_id_seq'::regclass)
pin | character varying |
occurred_at | timestamp with time zone |
membership_id | bigint |
appointment_id | bigint |
created_at | timestamp with time zone |
created_by | json |
updated_at | timestamp with time zone |
updated_by | json |
customer_id | bigint |
state | character varying | not null default 'booked'::character varying
Indexes:
"booking_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"booking_appointment_id_fkey" FOREIGN KEY (appointment_id) REFERENCES appointment(id)
"booking_customer_id_fkey" FOREIGN KEY (customer_id) REFERENCES customer(id)
"booking_membership_id_fkey" FOREIGN KEY (membership_id) REFERENCES membership(id)
Referenced by:
TABLE "booking_decline_reason" CONSTRAINT "booking_decline_reason_booking_id_fkey" FOREIGN KEY (booking_id) REFERENCES booking(id)
I am trying to update the booking.update_at from the statemachine_history.updated_at
Letting you know that there is a one to many relationship between the 2 tables so i want to MAX(statemachine_history.updated_at)
My try is:
UPDATE booking SET updated_at=
(
SELECT MAX(updated_at)
FROM statemachine_history
WHERE schema_name='Booking'
AND identifier=id
GROUP BY identifier
);
However the bookings.updated_at becomes null