Yeah right... here's the test
import sys
sys.path.append("/Users/dmiller/Code/PyOE/resources/lib/SQLAlchemy/test")
from sqlalchemy import *
from testbase import AssertMixin
import testbase
import unittest, sys, datetime
db = testbase.db
from sqlalchemy import *
meta = BoundMetaData(db)
orders = Table('orders', meta,
Column('id', Integer, Sequence('order_id_seq'), primary_key = True),
Column('name', VARCHAR(50)),
)
items = Table('items', meta,
Column('id', Integer, Sequence('item_id_seq'), primary_key = True),
Column('order_id', Integer, ForeignKey(orders.c.id), nullable=False),
Column('name', VARCHAR(50)),
)
attributes = Table('attributes', meta,
Column('id', Integer, Sequence('attribute_id_seq'), primary_key = True),
Column('item_id', Integer, ForeignKey(items.c.id), nullable=False),
Column('name', VARCHAR(50)),
)
class SessionTest(AssertMixin):
def setUp(self):
meta.create_all()
def tearDown(self):
meta.drop_all()
def testdeletechildwithchild(self):
class Order(object): pass
class Item(object): pass
class Attribute(object): pass
attrMapper = mapper(Attribute, attributes)
itemMapper = mapper(Item, items, properties=dict(
attributes=relation(attrMapper, cascade="all,delete-orphan", backref="item")
))
orderMapper = mapper(Order, orders, properties=dict(
items=relation(itemMapper, cascade="all,delete-orphan", backref="order")
))
s = create_session()
order = Order()
s.save(order)
item = Item()
attr = Attribute()
item.attributes.append(attr)
order.items.append(item)
order.items.remove(item) # item is an orphan, but attr is not so flush() tries to save attr
s.flush()
assert item.id is None
assert attr.id is None
if __name__ == "__main__":
testbase.main()
On Aug 22, 2006, at 4:12 PM, dmiller wrote:
On Aug 18, 2006, at 12:29 PM, Michael Bayer wrote:
I addressed this issue the way I described earlier, that upon setting
up a relationship with "delete-orphan", a flag is tacked onto that
relatoinship's mapper that "orphans should be deleted" regardless of
the presence of any collection with which to cascade from.
Also, I did temporarily establish the rule we discussed where save()-
ing an orphaned entity will immediately raise an error; however, this
is incompatible with the "contextual session" since the entity is
added to the session upon construction and would render it impossible
to create new entities. you can see the commented-out check in the
changeset if youd like to uncomment it for experimentation.
what does happen now if you save() an orphaned entity is that it just
doesnt get saved. we could try to put the check further down at
flush time perhaps.
also, an interesting effect that I didnt think of came up from this,
which is that you now no longer can declare a "delete-orphan" cascade
rule on a self-referential relationship....since the root of a tree
structure is always an orphan. (of course, you could have a
circularly-linked structure, which probably requires the post_update
flag to be set; but i think that case is fringe enough that folks
could handle the deletes manually in that case.)
so check out the changeset, which includes the two new tests in a new
session.py unit test module:
http://www.sqlalchemy.org/trac/changeset/1807
OK, since I updated to r1815 things are starting to work better. But
I'm still having problems with orphaned items being saved. There's a
test case attached. Here's a quick overview:
s = create_session()
order = Order()
s.save(order)
item = Item()
attr = Attribute()
item.attributes.append(attr)
order.items.append(item)
order.items.remove(item)
s.flush()
The flush() attempts to insert the attribute associated with the item
even though the item was removed from the order and is therefore
transient. In other words item is an orphan, but flush() erroneously
tries to save attr since _is_orphan(attr) returns False. I would
suggest order.items.remove(item) would cascade to all of item's
children and register them as deleted as well (just like
order.items.append(item) causes all of item's children to be save
()'d ). Alternately, _is_orphan() could be modified to check all
parent items and detect if they are orphans as well.
Thanks.
~ Daniel
P.S. I never actually tried the first patch you had given me
regarding this issue, do you still want me to do that? I figured you
had patched it or something better into the trunk in r1807.
----------------------------------------------------------------------
---
Using Tomcat but need to do more? Need to support web services,
security?
Get stuff done quickly with pre-integrated technology to make your
job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache
Geronimo
http://sel.as-us.falkag.net/sel?
cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users
-------------------------------------------------------------------------
Using Tomcat but need to do more? Need to support web services, security?
Get stuff done quickly with pre-integrated technology to make your job easier
Download IBM WebSphere Application Server v.1.0.1 based on Apache Geronimo
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=120709&bid=263057&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users