Hello,

I have table "people" and table "accounts", each person can be payer
for any amount of accounts and reciever (perhaps I use wrong word, but
I can't find any better word) for any amount of accounts. This can be
done as two one-to-many relations. I also use backreferences.

And when I say some_account.payer_ = person_a, some_account.reciever_ also gets
changed and vice versa. Using person_a.payer.append(account) gives the
same result.

If dont's use backreferences it work's fine (of course using form
person_a.payer.append(account)), i.e. account.reciever_id doesn't get
changed and stills hold None.

If I remember correctly this was working fine back good old days of SqlAlchemy 
1.7
times. (But I like sqlalchemy 0.2.x more anyway :-)

Here's a test script (Also in an attach)
=====================================
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
=====================================
This scipt prints:

Task dump:

 UOWTask(12568752, Person/people/None)
  |- Save Person(12529168)
  |- Save Person(12529200)
  |       |-Process Person(12529168).reciever
  |       |-Process Person(12529200).reciever
  |       |-Process Account(12529488).payer_
  |       |-Process Person(12529168).payer
  |       |-Process Person(12529200).payer
  |       |-Process Account(12529488).reciever_
  | 
  |- UOWTask(12568912, Account/accounts/None)
  |   |- Save Account(12529488)
  |   |----
  | 
  |----



Execute complete

Account: id=1 name=123 payer_id=1 reciever_id=1 
====================================================
IMHO task dump with that long changelist on Save Person(12529200)
looks strange, perhaps problem lies somewhere in that place,
or do I have an error in my script?

-- 
Best regards,
 Vasily                          mailto:[EMAIL PROTECTED]



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