oh, duh. do it like this: for purchase in list(aItem.purchase): purchase.cbbottle = bItem
I leave it to you as an exercise why this is the case. On Jan 29, 2009, at 3:08 PM, Werner F. Bruhin wrote: > > Michael Bayer wrote: > > .. >> right there, purchasid 80 is not even in the list of items anymore. >> This is basically iterate the list, 80 is there, then flush(), then >> 80 >> is not there. this is all before anything has been "moved". so >> either the flush() does something, or just the move of item #79 >> affects something with #80. try it without the flush(), and >> alternatively try iterating through the list a second time without >> moving the items, see if 80 disappears. try without the delete- >> orphan too, perhaps thats triggering an event that is problematic. >> > Tried it with removing the flush() and not using the delete-orphan and > still the same behavior. > > I then added two more purchase records and now a pattern starts to > show, > i.e. every second one does not get moved. > > The slightly updated script (added a print itemB.purchase and removed > the flush) and the output: > > itemA = session.query(db.Cbbottle).get(keyA) > itemB = session.query(db.Cbbottle).get(keyB) > > print 'start to move from B to A' > print 'itemA id: %s' % itemA.cbbottleid > print 'no purch: %s' % len(itemB.purchase) > print itemB.purchase > > itemB = session.query(db.Cbbottle).get(keyB) > > for purch in itemB.purchase: > print 'purchasid: %s' % purch.purchaseid > print 'fk_cbbottleid: %s' % purch.fk_cbbottleid > purch.cbbottle = itemA > > session.commit() > > itemA = session.query(db.Cbbottle).get(keyA) > print 'after move from B to A' > print 'itemA id: %s' % itemA.cbbottleid > > for purch in itemA.purchase: > print 'purchasid: %s' % purch.purchaseid > print 'fk_cbbottleid: %s' % purch.fk_cbbottleid > > before move from B to A > itemB id: 174 > purchasid: 79 > fk_cbbottleid: 174 > purchasid: 80 > fk_cbbottleid: 174 > purchasid: 81 > fk_cbbottleid: 174 > purchasid: 82 > fk_cbbottleid: 174 > purchasid: 83 > fk_cbbottleid: 174 > start to move from B to A > itemA id: 175 > no purch: 5 > [Purchase(created=datetime.date(2009, 1, 29), fk_cbbottleid=174, > fk_cellarid=None, fk_currencyid=1, fk_supplierid=None, > purchased=datetime.datetime(2009, 1, 29, 0, 0), purchaseid=79, > purchaseprice=Decimal("0"), purchasepriceforeign=Decimal("0"), > purchasevalue=Decimal("0"), quantity=1, remarks=None, transferid=None, > updated=datetime.date(2009, 1, 29)), > Purchase(created=datetime.date(2009, 1, 29), fk_cbbottleid=174, > fk_cellarid=None, fk_currencyid=1, fk_supplierid=None, > purchased=datetime.datetime(2009, 1, 29, 0, 0), purchaseid=80, > purchaseprice=Decimal("0"), purchasepriceforeign=Decimal("0"), > purchasevalue=Decimal("0"), quantity=1, remarks=None, transferid=None, > updated=datetime.date(2009, 1, 29)), > Purchase(created=datetime.date(2009, 1, 29), fk_cbbottleid=174, > fk_cellarid=None, fk_currencyid=1, fk_supplierid=None, > purchased=datetime.datetime(2009, 1, 29, 0, 0), purchaseid=81, > purchaseprice=Decimal("0"), purchasepriceforeign=Decimal("0"), > purchasevalue=Decimal("0"), quantity=5, remarks=None, transferid=None, > updated=datetime.date(2009, 1, 29)), > Purchase(created=datetime.date(2009, 1, 29), fk_cbbottleid=174, > fk_cellarid=None, fk_currencyid=1, fk_supplierid=None, > purchased=datetime.datetime(2009, 1, 29, 0, 0), purchaseid=82, > purchaseprice=Decimal("0"), purchasepriceforeign=Decimal("0"), > purchasevalue=Decimal("0"), quantity=2, remarks=None, transferid=None, > updated=datetime.date(2009, 1, 29)), > Purchase(created=datetime.date(2009, 1, 29), fk_cbbottleid=174, > fk_cellarid=None, fk_currencyid=1, fk_supplierid=None, > purchased=datetime.datetime(2009, 1, 29, 0, 0), purchaseid=83, > purchaseprice=Decimal("0"), purchasepriceforeign=Decimal("0"), > purchasevalue=Decimal("0"), quantity=3, remarks=None, transferid=None, > updated=datetime.date(2009, 1, 29))] > purchasid: 79 > fk_cbbottleid: 174 > purchasid: 81 > fk_cbbottleid: 174 > purchasid: 83 > fk_cbbottleid: 174 > after move from B to A > itemA id: 175 > purchasid: 79 > fk_cbbottleid: 175 > purchasid: 81 > fk_cbbottleid: 175 > purchasid: 83 > fk_cbbottleid: 175 > > My work around is iterate over the purchase relation and get the > primary > keys, then do a query on the purchase table and move them. > > Following script does the trick but it is not quit as nice. > > keyA = 174 > keyB = 175 > > itemB = session.query(db.Cbbottle).get(keyB) > print 'before move from B to A' > print 'itemB id: %s' % itemB.cbbottleid > allPurch = [] > for purch in itemB.purchase: > allPurch.append(purch.purchaseid) > > print 'allPurch: %s' % allPurch > > itemA = session.query(db.Cbbottle).get(keyA) > itemB = session.query(db.Cbbottle).get(keyB) > > print 'start to move from B to A' > print 'itemA id: %s' % itemA.cbbottleid > print 'no purch: %s' % len(itemB.purchase) > > for purchId in allPurch: > purch = session.query(db.Purchase).get(purchId) > print 'purchasid: %s' % purch.purchaseid > print 'fk_cbbottleid: %s' % purch.fk_cbbottleid > purch.cbbottle = itemA > > session.commit() > > itemA = session.query(db.Cbbottle).get(keyA) > print 'after move from B to A' > print 'itemA id: %s' % itemA.cbbottleid > > for purch in itemA.purchase: > print 'purchasid: %s' % purch.purchaseid > print 'fk_cbbottleid: %s' % purch.fk_cbbottleid > > > Werner > > > --~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---