Hi Mike,
Thanks. I learning SQA and I am trying to get the general feeling.
I guess the idea is to use Python directly and Session take care of
the SQL in the background.
mOutOrder = mapper(dbOutOrder, tbl_outHeader, properties={'lines':
relation(dbOutLine, cascade="all, delete-orphan")})
mOutLine = mapper(dbOutLine, tbl_outLine)
session = Session()
order = session.query(dbOutOrder).get(2)
tmp_list = order.lines[:]
for line in tmp_list:
order.lines.remove(line)
for i in range(1,10):
line = dbOutLine()
order.lines.append(line)
i = 0
tmp_list = order.lines[:]
for line in tmp_list:
if i % 2 == 0:
order.lines.remove(line)
i += 1
session.commit()
-- jacek
On Oct 24, 6:27 pm, Michael Bayer <[EMAIL PROTECTED]> wrote:
> On Oct 24, 2008, at 9:08 PM, jack2318 wrote:
>
>
>
>
>
> > I am really sorry but I pressed POST before I was ready. So again the
> > code (without comments)
>
> > session = Session()
> > order = session.query(dbOutOrder).get(2)
>
> > for line in order.lines:
> > session.delete(line)
> > session.flush()
>
> > for i in range(1,10):
> > line = dbOutLine()
> > line.HEADER_id = order.id
> > line.ITEM_SKU_id = 1
> > line.QTY = 1
>
> > order.lines.append(line)
>
> > session.add(order)
> > session.flush()
>
> > i = 0
> > for line in order.lines:
> > if i % 2 == 0:
> > print "DELETE"
> > session.delete(line)
> > i += 1
> > session.commit()
>
> > and the mappers:
> > mOutOrder = mapper(dbOutOrder, tbl_outHeader, properties={'lines':
> > relation(dbOutLine)})
> > mOutLine = mapper(dbOutLine, tbl_outLine)
>
> > as soon as I get to commit I get error:
>
> > Unexpected error: <class
> > 'sqlalchemy.orm.exc.ConcurrentModificationError'> Deleted rowcount 5
> > does not match number of objects deleted 7
>
> > What I am doing wrong? Of course if I issue commit after first delete
> > everything is fine.
>
> session.delete(someobject) does not remove the object from
> collections. You're issuing delete() on some of the members of
> order.lines twice (delete() puts them back into the session after
> they've been removed). If you issue a commit(), the order.lines
> collection is expired so it refreshes the most current data on next
> access and therefore the problem is eliminated.
>
> Instead of issuing delete() on collection members, just remove them
> from the collection and use cascade="all, delete-orphan" on the
> order.lines relation.
>
> also "order" is already in the session; no need to add() it a second
> time.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---