"Invalid column expression" error message could be vastly improved

Issue #1476 resolved
Former user created an issue

When query(...) is run, it appears that if it cannot figure out what to do with its argument, then it prints the very confusing error message:

sqlalchemy.exc.InvalidRequestError: Invalid column expression ...

What I really needed it to say, in my case, is that "You have handed me a plain class that has not been properly hooked up to SQLAlchemy through an ORM mapper, so I don't know how to query it." It took me quite a bit of work to get from the error message to the fact that I had forgotten to have Pylons call init_model(). :-)

Of course, since there are lots of possible things that get handed to query(), and classes are only one of them, the message can't just handle my case; but the fact that it chooses instead to address another case — the possibility that someone is asking for a particular column? — just generates confusion.

If the message needs to remain short, it could be re-worded to something like:

sqlalchemy.exc.InvalidRequestError: query() has been called
with an object that does not identify a database table or query

Better yet (but maybe SQLAlchemy doesn't like long messages?) would be:

sqlalchemy.exc.InvalidRequestError: query() needs to be supplied
with a table name, column name, or a table or column object mapped
with the SQLAlchemy ORM; instead, it has been called with: <...object...>

Anyway, if the error message could stop talking about columns when the problem was that I hadn't made my web framework call my ORM mapper() command, it would take less time to debug it.

Here's some test code that causes the error:

import sqlalchemy
from sqlalchemy.orm import sessionmaker

engine = sqlalchemy.create_engine('sqlite:///:memory:')
Session = sessionmaker(bind=engine)

class Test(object):
    pass

Session().query(Test)

Comments (5)

  1. Mike Bayer repo owner

    To me, the error message indicates almost exactly what the problem is - the only change I might make is "invalid column or entity expression", so that it is clear that the two valid expression types are "column" or "entity". those two terms are commonly used, where "entity" means "mapped entity", so in this case it would indicate the entity Test was not mapped. the usage of mapper() is a fundamental SQLA concept documented ubiqitously so I don't think the message needs to reiterate that documentation.

  2. Former user Account Deleted

    I have checked out 1e6df0eeb7845e85e98ce334474e4c9ab8c10611, and it looks great! I think that the new message will help me a lot the next time I encounter it. (Which is pretty often, since I use several web frameworks and so I stay a bit fuzzy on which dials have to be set correctly on each one to get the mappers set up.) Thanks. SQLAlchemy is the most responsive open-source project I know of. Much better than mine, for example. :-) Thanks!

  3. Log in to comment