Sandro Dentella wrote:
>   I really feel like there should be a way to preserve the order of the
> data
>   returned from the database and a way to loop into the data even when you
>   don't know how many chained ralations are there and if the relation is
>   onetomany or manytoone.
>

well, the way youve made your relationships, there is an ordering inherent
in those controlled by their structure.

if you add an attribute like this:

m2 = mapper(Mail, mails)
rel = relation(m2, lazy=False, order_by=mails.c.mail_address)

now you get this query:

 SELECT users.city AS users_city, users.user_id AS users_user_id,
mails_1c2c.user_id AS mails_1c2c_user_id, mails_1c2c.mail_address AS
mails_1c2c_mail_address, users.user_last_name AS users_user_last_name,
city_4e.city AS city_4e_city, city_4e.country AS city_4e_country,
users.user_name AS users_user_name
FROM users LEFT OUTER JOIN mails AS mails_1c2c ON users.user_id =
mails_1c2c.user_id LEFT OUTER JOIN city AS city_4e ON city_4e.city =
users.city ORDER BY mail_address, mails_1c2c.mail_address

and setting "echo='debug'", you can see the results:

(u'Paris', 3, None, None, u'Smith', u'Paris', u'Paris', u'Axe')
(u'Milan', 1, 1, u'[EMAIL PROTECTED]', u'Patts', u'Milan', u'Italy', u'Sam')
(u'Madrid', 4, 4, u'[EMAIL PROTECTED]', u'Will', u'Madrid', u'Madrid', u'Ted')
(u'Rome', 2, 2, u'[EMAIL PROTECTED]', u'Watts', u'Rome', u'Italy', u'Sid')
(u'Madrid', 4, 4, u'[EMAIL PROTECTED]', u'Will', u'Madrid', u'Madrid',
u'Ted')

and i think thats the order you want.  SA then loops through those rows
and assembles the relationships in exactly that order, but because the
email addresses are attached to parent objects, namely "[EMAIL PROTECTED]" gets
attached to "Ted" who is already third in the list, that structure changes
the ordering.

SA cant really do anything about that....if youre looking to display a
result set that is ordered by email address, then you should select
directly from the Mail mapper and print in that direction:

mail = m2.select(order_by="mail_address")
for a in mail:
    u = m.get(a.user_id)
    print u.user_name, a.mail_address, u.cities.country





-------------------------------------------------------
This SF.Net email is sponsored by xPML, a groundbreaking scripting language
that extends applications into web and mobile media. Attend the live webcast
and join the prime developer group breaking into this new coding territory!
http://sel.as-us.falkag.net/sel?cmd=lnk&kid=110944&bid=241720&dat=121642
_______________________________________________
Sqlalchemy-users mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/sqlalchemy-users

Reply via email to