Since the id will only be available after a database insert; some flavor of session.flush() is needed. I like to keep object constructors limited to actual object construction; so putting it in get_person() after instantiating the person seems best approach.
-- Mike Conley On Mon, Jun 15, 2009 at 4:27 PM, JanW <jan.wu...@gmail.com> wrote: > > I am trying to access an objects primary key either in the constructor > (preferred) or right after the object is created. And I would like to > use a scoped session for this. Below is some dummy code to illustrate > what I'm trying to do. > > I defined the get_person function to first query the database to see > if the object already exists. If not it creates a new object. > > As is, the code will print 2 lines with in both cases 'None' as id. > > My question is, is there a way to make SA flush this object to the > database as soon as possible? Making the session autoflush=True does > not help. > > It does work if I enable the Session.object_session(self).flush() line > in the constructor or in the get_person function, but this doesn't > seem like a clean way to do it, especially not in the constructor. Is > this the correct way to do it or does SA provide another, cleaner way? > I would have expected autoflush to do this. > > (I can't do session.flush() since in reality session is not in the > scope of either Person.__init__() or get_person(), they are both > defined in my module's __init__.py while session is initialized in the > script that imports the module.) > > Many thanks, > > Jan. > > ### demo code: > ###### > from sqlalchemy import * > from sqlalchemy.orm import * > > # Define SQLite databases > person_engine = create_engine('sqlite:///person_db.sqlite') > > # Define database structure > metadata=MetaData() > metadata.bind = person_engine > > person_table = Table( > 'person', metadata, > Column('id', Integer, primary_key=True), > Column('name', Text), > ) > > person_table.create() > > Session = scoped_session(sessionmaker()) > #Session = scoped_session(sessionmaker(autoflush=True)) > session = Session() > > class Person(object): > def __init__(self, name): > self.name = name > # Session.object_session(self).flush() > print "In constructor: id=%s"%self.id > > person_mapper = Session.mapper(Person, person_table) > > def get_person(name): > result = Person.query().filter_by(name=name).first() > if not result: > result = Person(name) > # Session.object_session(result).flush() > print "New person made: id=%s, name=%s"%(result.id, > result.name) > return result > > p = get_person("Ed Jones") > > session.commit() > session.close() > > > > --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "sqlalchemy" group. To post to this group, send email to sqlalchemy@googlegroups.com To unsubscribe from this group, send email to sqlalchemy+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en -~----------~----~----~----~------~----~------~--~---