Discussion:
instance is not bound to a session
nathan
2014-02-06 01:02:47 UTC
Permalink
Hi,

I'm looping writing "Media" objects with SA, which are just rows in the db that represent media files. The same "Account" object/instance is used during each iteration of the loop and contains the user's username, among other things.

account = DBSession.query(Account).filter(Account.username=='username')
for fieldstorage in file_list:
filename = fieldstorage.filename
try:
media = Media()
DBSession.add(media)
media.create(account, filename)
transaction.commit()
except ...

What's happening is I'm getting the following exception on the 2nd iteration of the loop after I've committed one "Media" object:

Instance <Account at 0x1096bbb90> is not bound to a Session; attribute refresh operation cannot proceed

Please let me know if you need more info.

Thanks!
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Jonathan Vanasco
2014-02-06 01:36:26 UTC
Permalink
When you called 'commit', the 'account' object got expired.

There are a few ways in the FAQ or Wiki to deal with this. IIRC, you can
`merge` objects back into the session or `refresh` the object. One or both
might hit the database.

It would probably be better to change your code a bit, so you only commit
once. IMHO If your database supports savepoints, that would probably be
the best option -- then you could have a list of Success and Fails, and not
have one error kill the entire transaction.
--
You received this message because you are subscribed to the Google Groups "sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email to sqlalchemy+***@googlegroups.com.
To post to this group, send email to ***@googlegroups.com.
Visit this group at http://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/groups/opt_out.
Loading...