yeah i can tell you exactly why this is this way.

when the backref is created, the 0.1 series just re-used the join conditions from the original relation (in the case of a primary and secondary join on a many-to-many, it reverses them).  in 0.2 there were some problems with that....more complicated customized join conditions dont necessarily apply in reverse.

so for the moment you have to go more explicitly, if you have explicit join conditions:

properties = {
    'payer' : relation(
        Account,
        primaryjoin=(\
            accounts.c.payer_id==people.c.id),
        backref=backref('payer_', primaryjoin=accounts.c.payer_id==people.c.id)),
    'reciever' : relation(
        Account,
        primaryjoin=(\
            accounts.c.reciever_id==people.c.id),
        backref=backref('reciever_', primaryjoin=accounts.c.reciever_id==people.c.id))}

also the "foreignkey" parameter is usually not needed unless youre doing a self-referential mapper.

On Jul 20, 2006, at 11:14 AM, Vasily Sulatskov wrote:

from sqlalchemy import *


class SqlStrMixing( object ):

    def __init__(self, **kwargs):

        for key, value in kwargs.iteritems():

            setattr(self, key, value)


    def __str__( self ):

        s = [ self.__class__.__name__ + ': ' ]


        for c in self.c:

            s.append( u'%s=%s ' % ( c.key, getattr(self, c.key) ) )

        result = ''.join(s).encode( 'cp866' )


        return result


engine_uri = 'sqlite:///'


metadata = BoundMetaData(engine_uri, convert_unicode=True)


people = Table('people', metadata, 

    Column('id', Integer, 

        primary_key=True), 

    Column('name', Unicode(100)))


accounts = Table('accounts', metadata, 

    Column('id', Integer, 

        primary_key=True), 

    Column('name', Unicode(100)),

    Column('payer_id', Integer, 

        ForeignKey(people.c.id), index=True, default=None),

    Column('reciever_id', Integer, 

        ForeignKey(people.c.id), index=True, default=None))


metadata.create_all()


class Person(SqlStrMixing):

    pass


class Account(SqlStrMixing):

    pass


mapper(Account, accounts)


# I use underscore to avoid confusion

properties = { 

    'payer' : relation(

        Account,

        primaryjoin=(\

            accounts.c.payer_id==people.c.id),

        foreignkey=accounts.c.payer_id,

        backref='payer_'),

    'reciever' : relation(

        Account,

        primaryjoin=(\

            accounts.c.reciever_id==people.c.id),

        foreignkey=accounts.c.reciever_id,

        backref='reciever_')}


counteragent_mapper = mapper(Person, 

    people,

    properties=properties)


session = create_session(echo_uow=True)


jack = Person(name='Jack')

john = Person(name='John')

acc = Account(name='123')


acc.payer_ = jack


session.save(jack)

session.save(john)

session.save(acc)


session.flush()

session.close()


session = create_session()


query = session.query(Account)


for a in query.select():

    print a


-------------------------------------------------------------------------
Take Surveys. Earn Cash. Influence the Future of IT
Join SourceForge.net's Techsay panel and you'll get the chance to share your
opinions on IT & business topics through brief surveys -- and earn cash
http://www.techsay.com/default.php?page=join.php&p=sourceforge&CID=DEVDEV
_______________________________________________
Sqlalchemy-users mailing list
Sqlalchemy-users@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to