Zope-SQLAlchemy extension

57 views
Skip to first unread message

Clemens Herschel, III

unread,
Nov 16, 2012, 11:40:12 AM11/16/12
to sqlal...@googlegroups.com
 In a pyramid application:
In models.py: DBSession= scoped_session(sessionmaker(extensions=ZopeTransactionExtension()))
 In view:   dbsession = DBSession
               def add(request):
    #get implant1
               dbsession.add(implant)
               transaction.commit()
             dbsession = DBsession
This works fine on first add. On second call to add, the first implant object is updated rather than an object added as I would expect from the sqlalchemy session docs .  Using a new session after the commit is suggested in the zope-sqlalchemy docs to achieve this. The user in this application might do repeated adds into many tables 
in one request .
So because I  want to use more than one session per request, I should not use Zope-SQLAlchemy extension but SQLAlchemy  ScopedSession helper class. Is that correct? Please excuse any misuse of terminology. Thanks for pointing me in the right direction.
Using SQLAlchemy0.7.3
zope-sqlalchemy0.6.1

Michael Bayer

unread,
Nov 16, 2012, 9:00:03 PM11/16/12
to sqlal...@googlegroups.com
there's misunderstanding here - whether an INSERT or UPDATE is emitted is based on the state of the object passed to session.add(), whether transient (INSERT) or detached (will become persistent and UPDATE will be emitted for changes).   These states are documented here: http://docs.sqlalchemy.org/en/latest/orm/session.html#quickie-intro-to-object-states .    A transient object is only created via the constructor, "implant = Implant()", or if a detached "implant" object is made transient again using the make_transient() helper function.   Else your object is "detached" or "persistent" and refers to an existing row and can only invoke an UPDATE or DELETE statement.

The code here doesn't describe what "implant" is, or where it comes from, or what exactly "doesn't work fine" means as I don't see a second call to "add()" here and I don't have detail on the context in which this code excerpt is called.    

There's a vague suggestion here that perhaps you're doing some kind of master/slave/replication type of thing "the user might do repeated adds into many tables in one request", there's different ways to approach that of which using multiple Session objects bound to each engine is just one, though a single Session can be made to refer to multiple engines simultaneously (see http://techspot.zzzeek.org/2012/01/11/django-style-database-routers-in-sqlalchemy/ for one example).

make_transient() may be a worthy helper here or even just a simple "copy()" method on your "implant" object to make new transient instances.

Clemens Herschel

unread,
Nov 21, 2012, 10:34:03 AM11/21/12
to sqlal...@googlegroups.com
Thanks for your help. There was a misunderstanding and general confusion on my part. make transient() IS a worthy helper and  I fixed  my zope transactions error as well.

--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To post to this group, send email to sqlal...@googlegroups.com.
To unsubscribe from this group, send email to sqlalchemy+...@googlegroups.com.
For more options, visit this group at http://groups.google.com/group/sqlalchemy?hl=en.

Reply all
Reply to author
Forward
0 new messages