Sorry for the confusing subject.  If I map against a select in which
one of the tables has a foreign key whose default value is set to a
clause involving another table's column (also in the select), and that
column is labeled, the correct values are inserted, but an exception
is raised when accessing the foreign key attribute on the instance
(the instance is in an invalid state).

Minimalist example:

# Boilerplate setup:
engine = create_engine('sqlite:///test.db', echo=True)
metadata = MetaData(bind=engine)
Session = sessionmaker(bind=engine)
session = Session()

# The tables:
a_table = Table('a', metadata,
    Column('a_id', Integer, primary_key=True),
    Column('a_val', Integer, default=0) # Only here to prevent bare
insert statement
)

# Don't get caught up on why we might want to do this... :)
max_a_id = func.max(a_table.c.a_id).select().as_scalar()

b_table = Table('b', metadata,
    Column('b_id', None, ForeignKey(a_table.c.a_id),
        primary_key=True, default=max_a_id),
    Column('b_val', Integer, default=0) # Only here to prevent bare
insert statement
)

class AB(object):
    pass

# Or this!
s = select([
    a_table, # Get a_id, a_val...
    b_table, # Get b_id, b_val...
    a_table.c.a_id.label('the_id_of_a') # Get a_id as the_id_of_a...
]).alias('ab')

ab_mapper = mapper(AB, s)

ab = AB()
session.save(ab)
session.commit()

ab.the_id_of_a == 1
ab.a_id == None # Wrong :(
ab.b_id # Exception! (Traceback at bottom)

Aaand that's all.  It might seem a little convoluted but it also seems
like it should work... especially since the expected values are
inserted into the database.

Thoughts?

Traceback:

/Users/exogen/Projects/revision-env/lib/python2.5/SQLAlchemy-0.4.0-
py2.5.egg/sqlalchemy/orm/attributes.py in __get__(self, obj, owner)
     38         if obj is None:
     39             return self
---> 40         return self.impl.get(obj._state)
     41
     42     def get_history(self, obj, **kwargs):

/Users/exogen/Projects/revision-env/lib/python2.5/SQLAlchemy-0.4.0-
py2.5.egg/sqlalchemy/orm/attributes.py in get(self, state, passive)
    242                 if passive:
    243                     return PASSIVE_NORESULT
--> 244                 value = callable_()
    245                 if value is not ATTR_WAS_SET:
    246                     return self.set_committed_value(state,
value)

/Users/exogen/Projects/revision-env/lib/python2.5/SQLAlchemy-0.4.0-
py2.5.egg/sqlalchemy/orm/strategies.py in lazyload()
    217                 row = result.fetchone()
    218                 for prop in group:
--> 219
sessionlib.attribute_manager.set_committed_value(instance, prop.key,
row[prop.columns[0]])
    220                 return attributes.ATTR_WAS_SET
    221             finally:

<type 'exceptions.TypeError'>: 'NoneType' object is unsubscriptable


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to