Upgrading from 1.3.23 to 1.4.x subquery seems stricter

118 views
Skip to first unread message

gbr

unread,
May 29, 2022, 12:32:39 AM5/29/22
to sqlalchemy
In SQLA 1.3.x, I used to follow this pattern to construct a query:

```
def load_something(... sort=False):
   qry = select(columns=..., from_obj=...)
   if sort:
     qry = qry.order_by(desc(qry.c.created_on))
   if ...
     qry = qry...
```

but in 1.4.0 and 1.4.37 (I guess the versions in between as well), this doesn't work any more.

The error message that I now get relates to aliases being created which don't actually exist:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) missing FROM-clause entry for table "anon_2"
LINE 4: WHERE task.type = 'xyz' ORDER BY anon_2.created_on DESC, ...

I've been using this pattern quite a lot. Is there a way to reinstate this functionality or monkey patch my version until I changed the code?

Mike Bayer

unread,
May 29, 2022, 11:16:25 AM5/29/22
to noreply-spamdigest via sqlalchemy


On Sun, May 29, 2022, at 12:32 AM, gbr wrote:
In SQLA 1.3.x, I used to follow this pattern to construct a query:

```
def load_something(... sort=False):
   qry = select(columns=..., from_obj=...)
   if sort:
     qry = qry.order_by(desc(qry.c.created_on))
   if ...
     qry = qry...
```

but in 1.4.0 and 1.4.37 (I guess the versions in between as well), this doesn't work any more.

as it shoudln't, because it's wrong :)

what you should do is this:

qry = select(*columns).select_from(<from_obj>)
if sort:
    qry = qry.order_by(desc(qry.selected_columns.created_on))


see https://docs.sqlalchemy.org/en/14/changelog/migration_14.html#a-select-statement-is-no-longer-implicitly-considered-to-be-a-from-clause for background on why using "qry.c" was always wrong and how 1.4 / 2.0 are being changed to prevent this incorrect usage.





The error message that I now get relates to aliases being created which don't actually exist:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.UndefinedTable) missing FROM-clause entry for table "anon_2"
LINE 4: WHERE task.type = 'xyz' ORDER BY anon_2.created_on DESC, ...

I've been using this pattern quite a lot. Is there a way to reinstate this functionality or monkey patch my version until I changed the code?


--
SQLAlchemy -
The Python SQL Toolkit and Object Relational Mapper
 
 
To post example code, please provide an MCVE: Minimal, Complete, and Verifiable Example. See http://stackoverflow.com/help/mcve for a full description.
---
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+...@googlegroups.com.

Reply all
Reply to author
Forward
0 new messages