My db is postgresql. I have 4 tables and need to copy id of "payment_option" record to column in "settlement" table.
Quick info about model: Account has N invoices, Invoice has N settlements. Settlement has one of four payment options that are currently determined by column with string (PAYPAL, STRIPE, LEGACY, WIREPAYMENT). Account has N payment_options (always 4 in current state. Each type once) and each of these payment options has again one of those types in a column as string (PAYPAL, STRIPE, LEGACY, WIREPAYMENT). I need to add fkey pointing from settlement table to payment_option table that I can find settlement->invoice->account->payment_option
For this I need to do multiple join and do something like "UPDATE JOIN" but I am failing for some reason. My statement updates all settlement.payment_option_id on id of the first payment_option but not on id of payment_option bound through the account and invoice tables.
update settlement
set payment_option_id = P.id
from payment_option as P
inner join invoice as I on P.account_id = I.account_id
inner join settlement as S on S.invoice_id = I.id
where S.payment_option_type = P.type;
How to write it correctly and what is wrong? I think issue is in "SET" becuse when I write select with following joins it works as expected
Thanks Lukas