Unmapped columns with server-side defaults cause erroneous UnmappedColumnError

Issue #1995 resolved
Former user created an issue

This pertains to SQLAlchemy version 0.6.5.

If a table has a column with a server-side default, and that column is not mapped, then, e.g., when a new object is inserted, an UnmappedColumnError is thrown.

What is going on: sql.compiler.SQLCompiler._get_colparams puts all table columns with server-side defaults in the postfetch list. Normally this makes sense, defaulted columns should be invalidated, since we don't really know what value the server is going to give them. But when orm.mapper.Mapper._postfetch goes to invalidate the columns, if the column is not actually mapped, the error gets triggered.

I'm not yet smart enough to know whether the fix involves fixing SQLCompiler (so that it doesn't put the offending column in the postfetch list) or fixing the Mapper (to ignore postfetch columns which aren't mapped.)

Comments (7)

  1. Former user Account Deleted

    PS: A fairly hokey work-around is to assign the offending column both a server-side default and a client-side default.

    (In this case, the client-side default, of course, is what actually gets used when accessing the db through SQLAlchemy).

  2. Mike Bayer repo owner

    probably the latter approach. if its not mapped, mapper shouldn't do anything with it.

  3. Former user Account Deleted

    Replying to zzzeek:

    probably the latter approach. if its not mapped, mapper shouldn't do anything with it.

    That makes perfect sense when you say it. Thanks, Mike, for taking a look!

  4. Mike Bayer repo owner
    • changed milestone to 0.6.7

    weird, this should have been bumped up.

    diff -r 4f1274fc1fc675e2a482b68d658b36597f243c31 lib/sqlalchemy/orm/mapper.py
    --- a/lib/sqlalchemy/orm/mapper.py  Wed Feb 02 19:03:20 2011 -0500
    +++ b/lib/sqlalchemy/orm/mapper.py  Thu Feb 03 10:47:06 2011 -0500
    @@ -1937,7 +1937,7 @@
             if postfetch_cols:
                 state.expire_attributes(state.dict, 
                                     [self._columntoproperty[c](self._columntoproperty[c).key 
    -                                for c in postfetch_cols]
    +                                for c in postfetch_cols if c in self._columntoproperty]
                                 )
    
             # synchronize newly inserted ids from one table to the next
    
  5. Log in to comment