I wrote:
> Hi,
> 
> Does accessing a backref always have to issue SQL, even if 
> the object to
> be loaded already exists in the identity map? For example, if I have a
> many-to-one lazy-loaded relationship from Master to Detail with a
> backref, the statement "master.details[0].master" will issue 
> SQL for the
> '.master' backref, even though it already exists in the 
> session. I know
>
> [SNIP]
> 
> So "query.get" doesn't issue a query, but "master.details[0].master"
> does. Is there any way of making the backref use query.get, 
> and thereby
> use the identity map?
> 

I delved into the source to find out how this works, and I see that the
LazyLoader strategy has an attribute 'use_get' which is meant to do
exactly this. However the test to see whether the lazywhere clause is
the same as the mapper's get() clause is failing:

In [1]: import satest2

In [2]: s = satest2.Detail.master.property.strategy

In [3]: s.mapper._get_clause[0].compare(s._LazyLoader__lazywhere)
Out[3]: False

In [4]: print s.mapper._get_clause[0]
master.id = ?

In [5]: print s._LazyLoader__lazywhere
master.id = ?

In [6]: print s.mapper._get_clause[0].left
master.id

In [7]: print s._LazyLoader__lazywhere.left
master.id

In [8]: print
s.mapper._get_clause[0].left.compare(s._LazyLoader__lazywhere.left)
False

So even though both clauses are binary expressions representing
"master.id = ?", the master.id in each case is different.

On the offchance, I changed the foreign key definition from:

    master_id = sa.Column(sa.Integer, sa.ForeignKey(Master.id))

to

    master_id = sa.Column(sa.Integer,
sa.ForeignKey(Master.__table__.c.id))

...and now it seems to work! So is this a bug?

Thanks,

Simon

--~--~---------~--~----~------------~-------~--~----~
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 
sqlalchemy+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/sqlalchemy?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to