[sqlalchemy] Re: Correct way of moving relation

2009-01-30 Thread Werner F. Bruhin

Michael,

Michael Bayer wrote:
> 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.
>   
aItem.purchase is an instrumented list and as such is mutable, is that 
the right conclusion?

Thanks for your help
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Michael Bayer

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(d

[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Werner F. Bruhin

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

[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Michael Bayer


On Jan 29, 2009, at 1:19 PM, Werner F. Bruhin wrote:

>
> Michael,
>
> I run the following script and initially had the either my application
> and/or the IB Expert database tool (for Firebird SQL v 2.1) open at  
> the
> same time.  Now the following tests are done without any other task
> accessing the database.
>
> script:
> engine = db.sa.create_engine(dburl, encoding='utf8', echo=False)
> Session = db.sao.sessionmaker()
> Session.configure(bind=engine)
> session = Session()
>
> keyA = 174
> keyB = 175
>
> itemB = session.query(db.Cbbottle).get(keyB)
> print 'before move from B to A'
> print 'itemB id: %s' % itemB.cbbottleid
>
> for purch in itemB.purchase:
>print 'purchasid: %s' % purch.purchaseid
>print 'fk_cbbottleid: %s' % purch.fk_cbbottleid
>
> session.flush()
>
> 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
>
> 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
>
> The following is the output, note that purchasid "80" is not being  
> moved.
> before move from B to A
> itemB id: 175
> purchasid: 79
> fk_cbbottleid: 175
> purchasid: 80
> fk_cbbottleid: 175
> purchasid: 81
> fk_cbbottleid: 175
>
> start to move from B to A
> itemA id: 174
> purchasid: 79
> fk_cbbottleid: 175
> purchasid: 81
> fk_cbbottleid: 175

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.


--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread az

that sess.fulsh() in the middle there... if u move it up/down/out, 
will behaviour change? e.g. if u print the things in itemB.purchase 
just _After that flush - is 80 there or not?

On Thursday 29 January 2009 20:19:59 Werner F. Bruhin wrote:
> Michael,
>
> I run the following script and initially had the either my
> application and/or the IB Expert database tool (for Firebird SQL v
> 2.1) open at the same time.  Now the following tests are done
> without any other task accessing the database.
>
> script:
> engine = db.sa.create_engine(dburl, encoding='utf8', echo=False)
> Session = db.sao.sessionmaker()
> Session.configure(bind=engine)
> session = Session()
>
> keyA = 174
> keyB = 175
>
> itemB = session.query(db.Cbbottle).get(keyB)
> print 'before move from B to A'
> print 'itemB id: %s' % itemB.cbbottleid
>
> for purch in itemB.purchase:
> print 'purchasid: %s' % purch.purchaseid
> print 'fk_cbbottleid: %s' % purch.fk_cbbottleid
>
> session.flush()
>
> 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
>
> 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
>
> The following is the output, note that purchasid "80" is not being
> moved. before move from B to A
> itemB id: 175
> purchasid: 79
> fk_cbbottleid: 175
> purchasid: 80
> fk_cbbottleid: 175
> purchasid: 81
> fk_cbbottleid: 175
>
> start to move from B to A
> itemA id: 174
> purchasid: 79
> fk_cbbottleid: 175
> purchasid: 81
> fk_cbbottleid: 175
>
> after move from B to A
> itemA id: 174
> purchasid: 79
> fk_cbbottleid: 174
> purchasid: 81
> fk_cbbottleid: 174
>
> Without doing other tasks on the database I run the same script
> again, and get this output ("80" is now moved).
> before move from B to A
> itemB id: 175
> purchasid: 80
> fk_cbbottleid: 175
>
> start to move from B to A
> itemA id: 174
> purchasid: 80
> fk_cbbottleid: 175
>
> after move from B to A
> itemA id: 174
> purchasid: 79
> fk_cbbottleid: 174
> purchasid: 80
> fk_cbbottleid: 174
> purchasid: 81
> fk_cbbottleid: 174
>
> If I run the script again nothing is moved (which is obviously
> correct) and all shows under fk_cbbottleid "174", now I change the
> keyA and keyB variable and reverse the values as follows:
> keyA = 175
> keyB = 174
>
> Now I get this and again "80" is not moved.
> before move from B to A
> itemB id: 174
> purchasid: 79
> fk_cbbottleid: 174
> purchasid: 80
> fk_cbbottleid: 174
> purchasid: 81
> fk_cbbottleid: 174
>
> start to move from B to A
> itemA id: 175
> purchasid: 79
> fk_cbbottleid: 174
> purchasid: 81
> fk_cbbottleid: 174
>
> after move from B to A
> itemA id: 175
> purchasid: 79
> fk_cbbottleid: 175
> purchasid: 81
> fk_cbbottleid: 175
>
> I can repeat this again and again and it is always "80" which does
> not move the first time.
>
> Any ideas on what I can do to find out what is causing this row not
> to move would be very much appreciated.
>
> 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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Werner F. Bruhin

Michael,

I run the following script and initially had the either my application 
and/or the IB Expert database tool (for Firebird SQL v 2.1) open at the 
same time.  Now the following tests are done without any other task 
accessing the database.

script:
engine = db.sa.create_engine(dburl, encoding='utf8', echo=False)
Session = db.sao.sessionmaker()
Session.configure(bind=engine)
session = Session()

keyA = 174
keyB = 175

itemB = session.query(db.Cbbottle).get(keyB)
print 'before move from B to A'
print 'itemB id: %s' % itemB.cbbottleid

for purch in itemB.purchase:
print 'purchasid: %s' % purch.purchaseid
print 'fk_cbbottleid: %s' % purch.fk_cbbottleid

session.flush()

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

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

The following is the output, note that purchasid "80" is not being moved.
before move from B to A
itemB id: 175
purchasid: 79
fk_cbbottleid: 175
purchasid: 80
fk_cbbottleid: 175
purchasid: 81
fk_cbbottleid: 175

start to move from B to A
itemA id: 174
purchasid: 79
fk_cbbottleid: 175
purchasid: 81
fk_cbbottleid: 175

after move from B to A
itemA id: 174
purchasid: 79
fk_cbbottleid: 174
purchasid: 81
fk_cbbottleid: 174

Without doing other tasks on the database I run the same script again, 
and get this output ("80" is now moved).
before move from B to A
itemB id: 175
purchasid: 80
fk_cbbottleid: 175

start to move from B to A
itemA id: 174
purchasid: 80
fk_cbbottleid: 175

after move from B to A
itemA id: 174
purchasid: 79
fk_cbbottleid: 174
purchasid: 80
fk_cbbottleid: 174
purchasid: 81
fk_cbbottleid: 174

If I run the script again nothing is moved (which is obviously correct) 
and all shows under fk_cbbottleid "174", now I change the keyA and keyB 
variable and reverse the values as follows:
keyA = 175
keyB = 174

Now I get this and again "80" is not moved.
before move from B to A
itemB id: 174
purchasid: 79
fk_cbbottleid: 174
purchasid: 80
fk_cbbottleid: 174
purchasid: 81
fk_cbbottleid: 174

start to move from B to A
itemA id: 175
purchasid: 79
fk_cbbottleid: 174
purchasid: 81
fk_cbbottleid: 174

after move from B to A
itemA id: 175
purchasid: 79
fk_cbbottleid: 175
purchasid: 81
fk_cbbottleid: 175

I can repeat this again and again and it is always "80" which does not 
move the first time.

Any ideas on what I can do to find out what is causing this row not to 
move would be very much appreciated.

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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Werner F. Bruhin

Michael,

Michael Bayer wrote:

...
>
> there's no need to reassign the FK column yourself and the original  
> pattern you're using is correct.  that only one item in the list is  
> the exception suggests something else is changing its state again  
> further down the road.
>   
Thanks for the quick reply, will setup an isolated test script and see 
if I can figure out what I am doing wrong.

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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Michael Bayer


On Jan 29, 2009, at 11:32 AM, Werner F. Bruhin wrote:

>
> Werner F. Bruhin wrote:
>> I have some items which are related but I need to change it so they
>> related to another item.
>>
>> Before getting myself in a mess (as I need to do this for a bunch of
>> tables) I wanted to check if the following approach is fine.
>>
>> I am using SA 0.5, ORM and declarative and the model is:
>>
>> class Cbbottle(Base):
>>__table__ = sa.Table(u'cbbottle', metadata,
>>sa.Column(u'cbbottleid', sa.Integer(),
>> sa.Sequence('gen_cbbottle_cbbottleid'), primary_key=True,  
>> nullable=False),
>>etc
>>)
>>
>> purchase = sao.relation('Purchase', cascade="all, delete,
>> delete-orphan", backref='cbbottle')
>>
>> aItem = session.query(db.Cbbottle).get(keyno)
>> bItem = session.query(db.Cbbottle).get(anotherkeyno)
>>
>> for purchase in aItem.purchase:
>>purchase.cbbottle = bItem
>>
>> session.commit()
>>
>> At this point I expect that aItem has no more "purchase" relations  
>> and
>> they are all related to bItem.
>>
>>
> I had a go at it and get this error on a few tables:
> c:\python25\lib\site-packages\sqlalchemy-0.5.2-py2.5.egg\sqlalchemy 
> \orm\properties.py:711:
> SAWarning: On Bottag.bothist, delete-orphan cascade is not supported  
> on
> a many-to-many or many-to-one relationship when single_parent is not
> set.   Set single_parent=True on the relation().
>  self._determine_direction()
>
> Changing the relations then makes it run without error but there is  
> one
> record being missed.
>
> I changed it slightly:
> aItem = session.query(db.Cbbottle).get(keyno)
> bItem = session.query(db.Cbbottle).get(anotherkeyno)
>
> for purchase in aItem.purchase:
>purchase.cbbottle = bItem
>purchase.fk_cbbottleid = bItem.cbbottleid
>
> session.commit()
>
> But for some reason one record is still not reassigned.  So, what I do
> is obviously not quit right.
>

there's no need to reassign the FK column yourself and the original  
pattern you're using is correct.  that only one item in the list is  
the exception suggests something else is changing its state again  
further down the road.





--~--~-~--~~~---~--~~
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
-~--~~~~--~~--~--~---



[sqlalchemy] Re: Correct way of moving relation

2009-01-29 Thread Werner F. Bruhin

Werner F. Bruhin wrote:
> I have some items which are related but I need to change it so they 
> related to another item.
>
> Before getting myself in a mess (as I need to do this for a bunch of 
> tables) I wanted to check if the following approach is fine.
>
> I am using SA 0.5, ORM and declarative and the model is:
>
> class Cbbottle(Base):
> __table__ = sa.Table(u'cbbottle', metadata,
> sa.Column(u'cbbottleid', sa.Integer(), 
> sa.Sequence('gen_cbbottle_cbbottleid'), primary_key=True, nullable=False),
> etc
> )
>
> purchase = sao.relation('Purchase', cascade="all, delete, 
> delete-orphan", backref='cbbottle')
>
> aItem = session.query(db.Cbbottle).get(keyno)
> bItem = session.query(db.Cbbottle).get(anotherkeyno)
>
> for purchase in aItem.purchase:
> purchase.cbbottle = bItem
>
> session.commit()
>
> At this point I expect that aItem has no more "purchase" relations and 
> they are all related to bItem.
>
>   
I had a go at it and get this error on a few tables:
c:\python25\lib\site-packages\sqlalchemy-0.5.2-py2.5.egg\sqlalchemy\orm\properties.py:711:
 
SAWarning: On Bottag.bothist, delete-orphan cascade is not supported on 
a many-to-many or many-to-one relationship when single_parent is not 
set.   Set single_parent=True on the relation().
  self._determine_direction()

Changing the relations then makes it run without error but there is one 
record being missed.

I changed it slightly:
aItem = session.query(db.Cbbottle).get(keyno)
bItem = session.query(db.Cbbottle).get(anotherkeyno)

for purchase in aItem.purchase:
purchase.cbbottle = bItem
purchase.fk_cbbottleid = bItem.cbbottleid

session.commit()

But for some reason one record is still not reassigned.  So, what I do 
is obviously not quit right.

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
-~--~~~~--~~--~--~---