Going back to the subject )) I get error (and no data updated) while using no_autoflush
/Users/sector119/PythonVirtualEnv/supplements/lib/python2.7/site-packages/sqlalchemy/orm/session.py:2122: SAWarning: Attribute history events accumulated on 1 previously clean instances within inner-flush event handlers have been reset, and will not result in database updates. Consider using set_committed_value() within inner-flush event handlers to avoid this warning. def reviews_after_insert_listener(mapper, connection, target): session = object_session(target) with session.no_autoflush: print repr(target.overview) target.overview.reviews_count += 1 target.overview.rating_sum += target.overview_rating target.overview.rating_count += 1 if target.overview_flavor_rating is not None: target.overview_flavor.rating_sum += target.overview_flavor_rating target.overview_flavor.rating_count += 1 event.listen(ProductOverviewReview, 'after_insert', reviews_after_insert_listener) четверг, 25 февраля 2016 г., 15:41:03 UTC+2 пользователь Simon King написал: > > Er, ok. There are simpler ways to avoid autoflush > > https://bitbucket.org/zzzeek/sqlalchemy/wiki/UsageRecipes/DisableAutoflush > > for example: > > session = sqlalchemy.orm.object_session(mapper) > with session.no_autoflush: > target.product.quantity += (value - oldvalue) > > ...but that still doesn't fix your underlying problem, which is that you > are assigning an unexpected object to your "label" property. I guess > FieldStorage comes from your web framework, and you need to extract the > actual value from that before assigning it to your mapped object. > > Simon > > On Thu, Feb 25, 2016 at 1:24 PM, sector119 <sect...@gmail.com > <javascript:>> wrote: > >> Thanks a lot, I add >> >> @event.listens_for(ProductFlavor, 'after_update') >> def quantity_before_update_listener(mapper, connection, target): >> quantity = >> select([func.coalesce(func.sum(ProductFlavor.__table__.c.quantity), >> 0)]).where( >> ProductFlavor.__table__.c.product_id == Product.__table__.c.id) >> connection.execute( >> Product.__table__.update().where(Product.__table__.c.id == >> target.product_id).values(quantity=quantity)) >> >> >> четверг, 25 февраля 2016 г., 11:52:50 UTC+2 пользователь Simon King >> написал: >>> >>> On Wed, Feb 24, 2016 at 9:51 PM, sector119 <sect...@gmail.com> wrote: >>> >>>> Hello! >>>> >>>> I have two models, Product and ProductFlavor with one-to-many >>>> relationship >>>> And I have a listener, which I want to update Product.quantity on >>>> ProductFlavor.quantity change: >>>> >>>> @event.listens_for(ProductFlavor.quantity, 'set') >>>> def quantity_set(target, value, oldvalue, initiator): >>>> if value != oldvalue: >>>> target.product.quantity += (value - oldvalue) >>>> >>>> >>>> But I get the following error: >>>> >>>> >>>> ProgrammingError: (raised as a result of Query-invoked autoflush; >>>> consider using a session.no_autoflush block if this flush is occurring >>>> prematurely) >>>> >>>> (psycopg2.ProgrammingError) can't adapt type 'instance' [SQL: 'UPDATE >>>> product_flavor SET label=%(label)s WHERE product_flavor.id = >>>> %(product_flavor_id)s'] [parameters: {'product_flavor_id': 4, 'label': >>>> FieldStorage('label', u'42bbebd1f7ba46b58d3d4b794b4b890e.png')}] >>>> >>>> What I'm doing wrong? >>>> >>> >>> It looks like you're assigning a non-string to your "label" column. This >>> isn't directly related to your attribute listener - the error would happen >>> even without the attribute listener when you called session.flush() or >>> session.commit(). The attribute listener is just causing the flush to >>> happen earlier presumably because "target.product" has not yet been loaded >>> from the database. >>> >>> Simon >>> >>> >>> -- >> 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 <javascript:>. >> To post to this group, send email to sqlal...@googlegroups.com >> <javascript:>. >> Visit this group at https://groups.google.com/group/sqlalchemy. >> For more options, visit https://groups.google.com/d/optout. >> > > -- 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+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.