I'm trying to execute a join query for 4 tables on postgres.
Table names:
scenarios_scenario
payments_invoice
payments_payment
payments_action
(all those weird names are generated by django -)))
Relations:
scenarios_scenario
[has many]payments_action
spayments_action
[has one]payments_invoice
payments_action
[has one]payments_payment
Below one is a working query,
SELECT payments_invoice.*,
(payments_payment.to_be_paid - payments_payment.paid) as remaining, \
payments_action.identificator
FROM payments_invoice
JOIN payments_payment
ON payments_invoice.action_id = payments_payment.action_id
AND payments_payment.full_payment=2
JOIN payments_action
ON payments_invoice.action_id = payments_action.id
AND payments_action.identificator = %s
I just want to retrieve a related field from another table and wrote another query like
SELECT
scenarios_scenario.title, payments_invoice.*, \
(payments_payment.to_be_paid - payments_payment.paid) as remaining, \
payments_action.identificator, payments_action.scenario_id
FROM payments_invoice
JOIN scenarios_scenario
ON scenarios_scenario.id = payments_action.scenario_id
JOIN payments_payment
ON payments_invoice.action_id = payments_payment.action_id
AND payments_payment.full_payment=2
JOIN payments_action
ON payments_invoice.action_id = payments_action.id
AND payments_action.identificator = 'EEE45667';
but facing with this error -
ERROR: missing FROM-clause entry for table "payments_action"
LINE 2: ...IN scenarios_scenario ON scenarios_scenario.id = payments_a...
^
Looked across SO for similar questions like this ("missing FROM-clause entry for table" error for a rails table query) but weren't able to find a way. Any help would be appreciated.
django.db.connection
as soon as i will be able to fix this query -)) – MeaveJOIN
to the bottom? You try to joinscenarios_scenario
before you joinedpayments_action
. – Intermediary