Warning about unresolved label when passing argument via text() to order_by

Issue #3428 resolved
Priit Laes created an issue

When passing following argument to order_by as text('(count=0)'), sqlalchemy-1.0.4 emits following warning. This seems to be a regression from 0.9 series.

sqlalchemy/sql/compiler.py:572: SAWarning: Can't resolve label reference '(stock = 0)'; converting to text() (this warning may be suppressed after 10 occurrences)

The order clause itself looks like this (stock and price come via labelled columns from joined tables): .order_by(Product.id, '(stock = 0)', 'price', Warehouse.min_days)

I have yet to extract a nice self-contained testcase for SQLAlchemy, but here's an example SQLFiddle http://sqlfiddle.com/#!15/fbfe4/5 which showcases the way I use the '(stock = 0)' as an argument to order_by.

Comments (2)

  1. Mike Bayer repo owner

    wait. You said, "via text()". but then here's the code:

        .order_by(Product.id, '(stock = 0)', 'price', Warehouse.min_days)
    

    that's not "text()", that's a string. Here's what text() would look like:

        .order_by(Product.id, text('(stock = 0)'), text('price'), Warehouse.min_days)
    

    e.g., by using text() yourself, you tell SQLAlchemy, "use this raw, as is". By sending a string, you use behavior that is subject to warnings, because ideally we don't want to auto-coerce anything to text(); it is an implicit behavior that people disagreed with. However, for an ORDER BY, we try to at least match the keyword given to a known label.

    So far this is not a regression. The warning is intentional and the full behavior here is documented at http://docs.sqlalchemy.org/en/latest/changelog/migration_10.html#warnings-emitted-when-coercing-full-sql-fragments-into-text.

    please confirm, thanks!

  2. Priit Laes reporter

    Oops. Indeed, it was a mistake as I had similar code in two places and I only fixed it in once place. (Guess I got sidetracked by the rollbacks).

  3. Log in to comment