2 messages in com.googlegroups.sqlalchemy[sqlalchemy] Alternate mappers for on...
FromSent OnAttachments
Hermann Himmelbauer20 Feb 2008 05:48 
Michael Bayer20 Feb 2008 08:21 
Subject:[sqlalchemy] Alternate mappers for one relation -> caching problem/bug?
From:Hermann Himmelbauer (dus@qwer.tk)
Date:02/20/2008 05:48:37 AM
List:com.googlegroups.sqlalchemy

Hi, I'm experiencing some object caching problems here, it seems. In my case I have 3 tables A,B,C. B relates tables A and C and holds some extra columns.

I defined a mapper for B with two properties: property "a" and property "c" with a backreference of "b":

mapper(B, table_B, properties={ 'a' : relation(A, backref='b'), 'c' : relation(C, backref='b')}

This way, I can access C from A via A.b[0].c Moreover, I can now set columns in B like that: A.b[0].mycol = 'xyz'

To make things more handy, I thought I'd create another wrapper for situations where I don't need the columns of B. So I created the following mapper:

mapper(C, table_C, properties = { 'a' : relation(A, secondary=table_B, backref='c'})

Now I add relations via the first mapper:

a=A(); c1 = C(); c2= C(); session.save(a); session.save(c1), session.save(c2) b1 = B() b1.a = a; b1.c = c1 b2 = B() b2.a = a; b2.c = c2

Everything is correctly inserted into the database.

When I now try to retrieve data via the "shortcut mapper", e.g.

list(a.c) I only get one object back instead of two, which is _very_ bad.

What's an appropriate solution to this? Do I somehow have to flush/recreate the cache? Or should I not use 2 mappers for the same relation?

Best Regards, Hermann