OK, here's another try that seems to work, using AssociationProxy.

model.py: --------------------------------

from sqlalchemy import *
from sqlalchemy.ext.assignmapper import assign_mapper
from sqlalchemy.ext.associationproxy import AssociationProxy
from turbogears.database import metadata, session
from elixir import Unicode, Entity, has_field, using_options, has_one
from elixir import has_many, belongs_to, has_and_belongs_to_many


class ThingOne(Entity):
     has_field('name', Unicode)
     twos = AssociationProxy('cats', 'two')
     using_options(tablename='thing_one')

class ThingTwo(Entity):
     has_field('name', Unicode)
     ones = AssociationProxy('cats', 'one')
     using_options(tablename='thing_two')

# using plain SA for the Association Object:

cat_table = Table('cats_in_hats', metadata,
     Column('one_id', Integer, ForeignKey('thing_one.id')),
     Column('two_id', Integer, ForeignKey('thing_two.id')),
     Column('sorter', Integer),
     PrimaryKeyConstraint('one_id', 'two_id')
     )

class Cat(object):
     pass

cat_mapper = assign_mapper(session.context, Cat, cat_table,
     properties=dict(one=relation(ThingOne, backref='cats'),
                     two=relation(ThingTwo, backref='cats')))

# Add the relationships to the Entities
ThingOne.mapper.add_property('cats', relation(Cat, lazy=False,
                                                
order_by=cat_table.c.sorter,
                                               cascade='all, delete- 
orphan'))
# ThingOne.mapper.add_property('twos', relation(ThingTwo,))
ThingTwo.mapper.add_property('cats', relation(Cat, lazy=False,
                                                
order_by=cat_table.c.sorter,
                                               cascade='all, delete- 
orphan'))

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

and tests are the same i think... (append works too, but again you  
can't set the sorter field that way)

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

spam = ThingOne(name='spam')
eggs = ThingOne(name='eggs')
foo = ThingTwo(name='foo')
bar = ThingTwo(name='bar')
session.flush()
Cat(one=spam, two=bar, sorter=1)
Cat(one=spam, two=foo, sorter=2)
session.flush()
spam.refresh()
bar.refresh()
assert bar in spam.twos
assert foo in spam.twos
assert spam in bar.ones
assert spam in foo.ones
# make sure they are sorted:
assert spam.twos[0] is bar
assert spam.twos[1] is foo


--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"SQLElixir" 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/sqlelixir?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to