wobsta wrote:
>
> On 4 Jul., 17:05, Michael Bayer <mike...@zzzcomputing.com> wrote:
>> remove the item using remove().  What's the "items must be in a list  
>> rule", the not nullable foreign key ?  thats what "delete-orphan"  
>> cascade is for, its in the tutorial and reference documentation.
>
> Thanks, this works for the simple case in my first example. However,
> consider inserting the item in two ordered lists. If you remove it by
> the list method in one list, the position information is properly
> updated for *this* list. But not for the other list. See the following
> example:

that's true, if you have a child item that can be an "orphan" from two
separate lists, and you trigger delete orphan on one of them, the other
collection has no idea anything has happened until its refreshed.

the ORM doesn't attempt to replicate the whole database in Python.  Once
you commit() the session, all collections and such are expired and you'll
see the correct data as it gets reloaded.  Although in this case I would
probably just take the extra step of detaching l2 from i3.  delete-orphan
only implies item removal management for the single relation() on which
it's set.




>
> ------------------------
> # -*- encoding: utf-8 -*-
>
> from sqlalchemy import create_engine, MetaData, Table, Column,
> Integer, Unicode, ForeignKey, UniqueConstraint
> from sqlalchemy.orm import sessionmaker, mapper, relation
> from sqlalchemy.ext.orderinglist import ordering_list
>
> metadata = MetaData()
>
> stock_table = Table("stock", metadata,
>                     Column("id", Integer, primary_key=True),
>                     Column("name", Unicode, unique=True))
>
> list_table = Table("list", metadata,
>                    Column("id", Integer, primary_key=True),
>                    Column("name", Unicode, unique=True))
>
> item_table = Table("item", metadata,
>                    Column("id", Integer, primary_key=True),
>                    Column("name", Unicode, unique=True),
>                    Column("stockpos", Integer),
>                    Column("stock_id", Integer, ForeignKey("stock.id"),
> nullable=False),
>                    Column("listpos", Integer),
>                    Column("list_id", Integer, ForeignKey("list.id"),
> nullable=False),
>                    UniqueConstraint("stock_id", "name"),
>                    UniqueConstraint("list_id", "name"))
>
>
> class Stock(object):
>
>     def __init__(self, name):
>         self.name = name
>
>     def __repr__(self):
>         return "<Stock %s>" % self.name
>
>
> class List(object):
>
>     def __init__(self, name):
>         self.name = name
>
>     def __repr__(self):
>         return "<List %s>" % self.name
>
>
> class Item(object):
>
>     def __init__(self, name, stock, list):
>         self.name = name
>         self.stock = stock
>         self.list = list
>
>     def __repr__(self):
>         return "<Item %s part of %s at position %s and %s at position
> %s>" % (self.name, self.stock, self.stockpos, self.list, self.listpos)
>
>
> mapper(Stock, stock_table,
>        properties={"items": relation(Item,
>                                      backref="stock",
>                                      order_by=[item_table.c.stockpos],
>                                      collection_class=ordering_list
> ("stockpos"),
>                                      cascade="save-
> update,merge,delete,delete-orphan")})
> mapper(List, list_table,
>        properties={"items": relation(Item,
>                                      backref="list",
>                                      order_by=[item_table.c.listpos],
>                                      collection_class=ordering_list
> ("listpos"),
>                                      cascade="save-
> update,merge,delete,delete-orphan")})
> mapper(Item, item_table)
>
>
> engine = create_engine("postgres:///list", echo=True)
> metadata.bind = engine
> stock_table.create()
> list_table.create()
> item_table.create()
>
> Session = sessionmaker(engine)
> session = Session()
> s = Stock(u"items")
> l1 = List(u"a")
> l2 = List(u"b")
> i1 = Item(u"i1", s, l1)
> i2 = Item(u"i2", s, l1)
> i3 = Item(u"i3", s, l2)
> i4 = Item(u"i4", s, l2)
> session.add(s)
> session.commit()
> # up to this point everything is fine
> s.items.remove(i3)
> session.commit()
> print i4 # listpos should be 0, but it is 1 (i.e. not updated)
> session.close()
> engine.dispose()
>
> >
>


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

Reply via email to