Hi,

I am using the recipe for multiple foreign keys to the same table at
http://www.sqlalchemy.org/trac/wiki/UsageRecipes/MultiFkRelations

I am also using SessionContext, and assign_mapper

It works well, except for one issue. The second object (ie that which
the second foreign key references) is only sometimes loaded, and other
times it is None.

Here is my code (with other fields removed):

stockreceipt_table = Table('stockreceipt', meta,
    Column('user_id', Integer, ForeignKey('user.id')),
    # Cancellation
    Column('cancelled_user_id', Integer, ForeignKey('user.id')),
)

class Receipt(object):
    """Stock Receipt"""

assign_mapper(ctx, Receipt, stockreceipt_table,
    properties=dict(
        user=relation(User, backref='receipts',
            primaryjoin=stockreceipt_table.c.user_id ==
users_table.c.id),
        cancelled_user=relation(User, backref='cancelled_receipts',
lazy=False,
            primaryjoin=stockreceipt_table.c.cancelled_user_id ==
users_table.c.id),
    )
)

There is also a table "user" with an id column, which is reverenced
here.

So in an example

# Connect to the database, and create the tables etc

>>> u = User(name='Ali')
>>> u.flush()
>>> r = Receipt(user_id=u.id)
>>> r.flush()
>>> r.user is u
True
>>> r.cancelled_user_id = u.id
>>> r.flush()
>>> r.cancelled_user is u
True

So in this example it works perfectly as expected. However in my code
(a single threaded  PyGTK application, it doesn't work all the time,
and r.cancelled_user returns None more often than not. When the
application is restarted, the value is always correct.

The way I found to force it to work was to explicitly look up the
value like:

class Receipt(object):
    """Stock Receipt"""
    # A hack to force the getting of the second object
    def get_cancelled_user_hack(self):
        return User.get_by(id=self.cancelled_user_id)
    cancelled_user_hack = property(get_cancelled_user_hack)

I realise this is ugly and breaks all sorts of things in what it
expects User to be able to do with sessions etc, but I could not find
another reliable way to do it. The fact that it occurs only sometimes
may imply some threading issues I guess, but I am not sure where to
look.

When echoing is turned on and r.cancelled_user is accessed after
flushing, a new query is not made (in the application), but it is made
in the example above.

Thank-you for any assistance,

Ali


--~--~---------~--~----~------------~-------~--~----~
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?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to