On Dec 5, 2007, at 1:56 PM, David Gardner wrote:
>
> I have three tables a(a query of a really), b, c
>
> a has a 1-many relationship with b
> c has a 1-many relationship with b
>
> What I would like to do is in my mapper for table c, is sort the order
> of rows from b by a.name.
> I don't know how to do this or if it is possible. What I have looks
> like:
>
> sql_a = select([table_a], table_a.c.col1='some
> value').alias('a_query')
>
> mapper(B, b_table, properties = {
> 'A' : relation(sql_a, lazy=False,
> primaryjoin=(sql_a.c.id==table_b.c.a_id))})
>
> mapper(C, c_table, properties = {
> 'Bs' : relation(B, backref='C', cascade='all, delete-orphan',
> order_by=[C.A.name, c_table.c.value1,
> c_table.c.value2]),
> })
>
> This gets me an "AttributeError: 'InstrumentedAttribute' object has no
> attribute 'name'"
this error is becuase C.A is not a gateway to the columns on the "A"
mapper.
>
>
> I have also tried:
> order_by=[A.name
> and
> order_by=[sql_a.c.name
>
> both get me this: ProgrammingError: (ProgrammingError) missing
> FROM-clause entry for table "sql_a.name"
this error is because the relation from C->B has no relationship to
"A"s table at all, and it is not used in the generated query. to have
"A"s table be a part of it in all cases, youd have to map A's column
onto B's mapper, which is fairly messy:
mapper(B, select([b_table, a.c.name], b_table.c.a_id==a.c.id))
you can try creating this mapper as a "non-primary" mapper and using
it for just the C->B relation:
s = select([b_table, a.c.name], b_table.c.a_id==a.c.id)
mapper(C, ctable, properties={
'bs':relation(mapper(B, s, non_primary=True), order_by=[s.c.name])
})
I havent tried the above myself so see if it works for you.
--~--~---------~--~----~------------~-------~--~----~
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
-~----------~----~----~----~------~----~------~--~---