On Nov 6, 2009, at 7:53 PM, David Gardner wrote:
>
> CREATE TABLE task (
> "name" text NOT NULL,
> asset text NOT NULL,
> parent_asset text,
> <more columns>
> CONSTRAINT "task_PK" PRIMARY KEY (asset, name),
> CONSTRAINT "task_parent_FK" FOREIGN KEY (parent_asset, "name")
> REFERENCES task (asset, "name") MATCH SIMPLE
> ON UPDATE CASCADE ON DELETE CASCADE);
>
> So I define my mapper like:
> mapper(Task,task_table, properties={
> 'Children' : relation(Task, backref=backref('Parent',
>
> remote_side=[task_table.c.asset, task_table.c.name]),
>
> order_by=task_table.c.asset,cascade='all', lazy=True)
> })
>
> the problem is when I do:
>
> task=session.query(Task).options(eagerload(Task.Children)).get
> (('test_project','test_task'))
>
> produces this in SQL:
> SELECT <columns>
> FROM task LEFT OUTER JOIN task AS task_1 ON task.asset =
> task_1.parent_asset AND task.name = task.name
> WHERE task.asset = %(param_1)s AND task.name = %(param_2)s ORDER BY
> task_1.asset
Theres a test case like this which had a behavioral change as of
0.5.5, but looking at that, eager loading doesn't come into the picture.
for that particular test, we add foreign_keys=[task.c.parent_asset] to
the many to one side, and foreign_keys=[None] to the one-to-many
side. You might need that here just in general.
But for eagerloading I actually don't think we have a solution for
that right now. the "task_1" comes into the ON clause by way of
clause adaption, which has a list of columns that it wants to
"adapt". So "task.name" is either in or not in the list.
I'm thinking of a completely bizarre hack which would be to add a
Column to the table with the same name as "name", but a different key,
then setting up primaryjoin using that. But I don't know if that
would do it.
Otherwise you might just take the easy route and say:
t2 = aliased(Task)
s.query(Task).join(t2, and_(Task.parent_asset==t2.asset,
Task.name==t2.name)).options(contains_eager("parent" , alias=t2))
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"sqlalchemy" group.
To post to this group, send email to [email protected]
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
-~----------~----~----~----~------~----~------~--~---