On Wed, Feb 24, 2016 at 9:51 PM, sector119 <sector...@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+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.