My question is about using Association Objects.  While they certainly
make some things more natural, it seems that there is a price to pay
for not being able to do:

an_items_keywords = item.keywords

but to instead have to write

an_items_keywords = [ik.keyword for ik in item.itemkeywords]

which just doesn't seem natural to me.

It seems by using a viewonly relationship in the mapper you can have
the best of both worlds by preserving the use of the former (read only)
while still having use of an association object when you need to store
additional information in the object.  The item mapper below seems to
preserve the ability to use item.keywords (again, read only) and I am
just wondering if this could be made more natural than the construction
below or if there is an easier way to achieve the same thing.

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

item_table = Table('item',metadata,
  Column('id', Integer, primary_key=True),
  Column('name',String(150)))

keyword_table = Table('keyword', metadata,
  Column('id', Integer, primary_key=True),
  Column('name', String(25), nullable=False))


itemkeyword_table = Table('item_keyword', metadata,
  Column('item_id', Integer,ForeignKey('item.id'), nullable=False),
  Column('keyword_id', Integer, ForeignKey('keyword.id'),
nullable=False),
  Column('timestamp', DateTime, default=datetime.datetime.now,
onupdate=datetime.datetime.now))


# class definitions
class Item(object):
    def __init__(self, name):
        self.name = name

class Keyword(object):
    def __init__(self, name):
        self.name = name

class ItemKeyword(object):
    pass

mapper(Item, item_table, properties = dict(itemkeywords =
relation(ItemKeyword, lazy=False, association=Keyword,
backref='items'), keywords = relation(Keyword,
primaryjoin=and_(keyword_table.c.id==itemkeyword_table.c.keyword_id,
item_table.c.id==itemkeyword_table.c.item_id),
viewonly=True, foreignkey=keyword_table.c.id),))

mapper(Keyword, keyword_table)

mapper(ItemKeyword, itemkeyword_table, primary_key =
[itemkeyword_table.c.item_uuid, itemkeyword_table.c.keyword_uuid])


--~--~---------~--~----~------------~-------~--~----~
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 [EMAIL PROTECTED]
For more options, visit this group at http://groups.google.com/group/sqlalchemy
-~----------~----~----~----~------~----~------~--~---

Reply via email to