> as soon as Ticket is persistent within the flush, the ticket.assigned_to
> relation will be "live" and will lazy load when accessed. no commit is
> needed.
mmh, in the following example, I can't use assigned_to within
after_flush.
Am I doing something wrong?
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Table, Column, ForeignKey, text, func
from sqlalchemy.types import *
from sqlalchemy.orm import relation, sessionmaker
from sqlalchemy.orm.interfaces import SessionExtension
Base = declarative_base()
Base.metadata.bind = 'sqlite://'
class User(Base):
__tablename__ = 'user'
id = Column(Integer, primary_key=True)
username = Column(String(20))
class Ticket(Base):
__tablename__ = 'ticket'
id = Column(Integer, primary_key=True)
assigned_to_id = Column(ForeignKey(User.id))
assigned_to = relation(User, primaryjoin = assigned_to_id ==
User.id,
lazy=True)
class SKSessionExtension(SessionExtension):
def after_flush(self, session, flush_context):
"""
implement the after-flush signal
"""
for new in session.new:
if isinstance(new, Ticket):
print "NEW: %s - assigned_to: %s" % ( new,
new.assigned_to)
Session = sessionmaker(bind=Base.metadata.bind)
session = Session(extension=SKSessionExtension())
Base.metadata.create_all()
#Base.metadata.bind.echo = True
user = User()
user.username = 'aaa'
session.add(user)
session.commit()
ticket = Ticket()
ticket.assigned_to_id = user.id
session.add(ticket)
session.flush()
print "AFTER FLUSH", ticket.assigned_to
session.commit()
print "AFTER COMMIT", ticket.assigned_to
------------------
that leads to this output:
NEW: <__main__.Ticket object at 0x84f51cc> - assigned_to: None
AFTER FLUSH None
AFTER COMMIT <__main__.User object at 0x84f060c>
sandro
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---