unhelpful error when refering to a table instead of a class in relationship.

Issue #2196 resolved
Former user created an issue

Hello,

I've just spent a couple of hours staring at an 'UnmappedClassError Table("name of table", one thousand lines of columns) is not mapped' error message and just figured out, that the mistake was to put the name of the table in relationships first argument, instead of the name of the class (using declarative). The error sent me looking for errors in the framework, incorrectly bound engines, stuff like that.

I've written a patch against orm/util.py that checks for sqla.schema.Table in util.py:class_mapper.

There is also a short bit of code that triggers the error in table_instead_of_mapper.py

Thank you for your time.

Comments (3)

  1. Mike Bayer repo owner
    • changed component to orm
    • changed milestone to 0.7.2

    It's pretty arbitrary to put special handling for a "Table" and not any number of other objects that can be placed there.

    A more generalized solution would just to be check for "type".

    diff -r e30d02c6259d5a2386fd836e27dee75e3a143bd2 lib/sqlalchemy/orm/util.py
    --- a/lib/sqlalchemy/orm/util.py    Thu Jun 16 12:09:45 2011 -0400
    +++ b/lib/sqlalchemy/orm/util.py    Sat Jun 18 10:35:03 2011 -0400
    @@ -543,6 +543,8 @@
             mapper = class_manager.mapper
    
         except exc.NO_STATE:
    +        if not isinstance(class_, type):
    +            raise sa_exc.ArgumentError("Class object expected, got '%r'." % class_)
             raise exc.UnmappedClassError(class_)
    
         if compile and mapperlib.module._new_mappers:
    
  2. Log in to comment