Hi,

in my following example I don't know how to properly delete items in
an ordered list. I can't call items.remove (as I would violate the
"items must be in a list rule"). If I delete the item, the list order
get's not updated. Is there a way to automate the necessary reorder
call for this case? Additinally, what is the proposed way to reorder
an ordered list such that the positions are updated automatically?

Best,


André


PS: I'm running SQLAlchemy 0.5.4p2 on Python 2.6. The program first
prints  <Item item2 part of <List list> at position 2>, which is
wrong, and after the explicit reorder call it prints the correct <Item
item2 part of <List list> at position 1>.

-------------------------------------------

# -*- 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()

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("position", Integer),
                   Column("name", Unicode, unique=True),
                   Column("list_id", Integer, ForeignKey("list.id"),
nullable=False),
                   UniqueConstraint("list_id", "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, list=None):
        self.name = name
        self.list = list

    def __repr__(self):
        return "<Item %s part of %s at position %s>" % (self.name,
self.list, self.position)


mapper(List, list_table,
       properties={"items": relation(Item,
                                     backref="list",
                                     order_by=[item_table.c.position],
                                     collection_class=ordering_list
("position"))})
mapper(Item, item_table)


engine = create_engine("postgres:///list", echo=False)
metadata.bind = engine
list_table.create()
item_table.create()

Session = sessionmaker(engine)
session = Session()
l = List(u"list")
i1 = Item(u"item1", l)
i2 = Item(u"item2", l)
i3 = Item(u"item3")
l.items.insert(1, i3)
session.add(l)
session.commit()

# everything is fine up to this point. Now I want to delete item1 from
the list:

# l.items.remove(i1) # will violate the not null constraint of
item.list_id

session.delete(i1) # will not update the positions of the other items!
print "before explicit reorder:", i2
l.items.reorder() # does fix the positions -- is this the proposed
solution???
print "after explicit reorder:", i2

# also: what is the proposed solution to reorder the items, say I want
to move i1 to the end of the list?

session.commit()
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