Postgres: missing FROM-clause entry for table
Asked Answered
M

5

20

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_actions
  • payments_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.

Meave answered 18/9, 2017 at 8:37 Comment(8)
Post your python code, please?Shulman
i have no problem with python. will use django.db.connection as soon as i will be able to fix this query -))Meave
Have you tried moving your first JOIN to the bottom? You try to join scenarios_scenario before you joined payments_action.Intermediary
thanks @Dmitriy -)) moved to bottom and this workedMeave
@Meave while there is nothing wrong with SQL, the ORM in django does a lot of this for you.Shulman
@Shulman ofcourse, but i don't feel comfortable with django's orm, i think it is designed for making things more difficult -))Meave
No, it's really not -- if you post your models, I'll be happy to rewrite your query, if you post your model classes.Shulman
here you are - gist.github.com/marmeladze/87075548b0fb8de1082e95b112603803Meave
B
28

In your first join 'payments_action' is not a known relation. Reorder your joins in a way that a new join only uses already 'defined' relations.

Here is a fiddle, demonstrating the issue:

http://sqlfiddle.com/#!17/ed147/5

Basilio answered 18/9, 2017 at 8:48 Comment(0)
T
7

Change the code so that you join each table before calling a column from it in another join. The postgres query planner reads the joins sequentially so that in your code table scenarios_scenario is being joined to table payments_invoice and is looking for a match with payments_action, but the query planner doesn't know what payments_action is yet. The new code should be:

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 payments_action 
ON (
    payments_invoice.action_id = payments_action.id 
    AND payments_action.identificator = 'EEE45667'    
)
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 
);
Tardif answered 18/9, 2017 at 9:0 Comment(0)
K
5

You are joining to table [scenarios_scenario] using a field from [payments_action].

The joins must be in sequence, i.e. you cannot reference fields from a table with the ON statement unless their tables precede the statement.

Hope that helps

Keratose answered 18/9, 2017 at 8:47 Comment(0)
S
1

I had similar error message but related to alias naming issue. For example the following sql query does not work and throws the same error missing FROM-clause entry for table:

select aDr.address_line1 from core_addresses as "aDr"

If you are using Capital letters in alias names, you must use double quotes or use snake_case instead. The following queries fix the issue:

select "aDr".address_line1 from core_addresses as "aDr" // added quotes
select a_dr.address_line1 from core_addresses as "a_dr" // snake case
Stepp answered 6/12, 2021 at 16:42 Comment(1)
This is such an important information, I did not knew that, thank you!Sibling
O
0

I got the same error below:

ERROR: missing FROM-clause entry for table "my"

When I didn't use a current_setting() to use a custom option as shown below:

SELECT my.num;

So, I used current_setting() as shown below, then I could use the custom option without error:

SELECT current_setting('my.num');
Ozieozkum answered 13/12, 2023 at 5:24 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.