On Dec 25, 2011, at 7:36 AM, VDK wrote: > I have the following piece of code: > > class ContributieNotas(object): > > class Admin(EntityAdmin): > verbose_name = 'Contributie notas' > verbose_name_plural = 'Contributienota\'s' > list_display = ['id', 'achternaam', 'tussenvoegsel', > 'voorletters', 'adres', > 'postcode', 'woonplaats','omschrijving', > 'tariefbedrag', 'adresenpc'] > > def setup_views(): > > q = select([model.Leden.id, model.Leden.achternaam, > model.Leden.tussenvoegsel, model.Leden.voorletters, > model.Leden.adres, model.Leden.postcode, > model.Leden.woonplaats, model.Tarieven.omschrijving, > model.Tarieven.tariefbedrag, > model.Leden.adresenpc], > whereclause = > and_(model.Leden.lidsoort_id.in_([1,2]), > model.Tarieven.id == model.Leden.lidsoort_id), > order_by = (model.Leden.achternaam)) > > q = q.alias('contributie_alle_leden') > > mapper(ContributieNotas, q)
First off is definitely analyze the issue in terms of SQL using echo=True. There's no default ORDER BY in modern SQLAlchemy so the primary key ordering is likely just the insert ordering of the table. This is not directly linked to the primary key except that surrogate integer primary keys are typically assigned sequentially as rows are inserted. Second, the ORDER BY here will be rendered inside an aliased select, "SELECT * FROM (SELECT * FROM ... ORDER BY ...) as contribute_alle_leden WHERE <criterion>". Generally this will maintain that ordering but not necessarily, if the selectable is part of a bigger context that again changes how the database merges rows into the final result set. So for the ordering of the final result, the ORDER BY should be on the outside. mapper() offers an option called order_by which will instruct the Query object to place this as the default ORDER BY in the absence of other orderings. It must be in terms of the mapped selectable, here the q.alias(): mapper(ContNot, q.c.achternaam) > > one of them is putting the order by clause in the mapper but without > result. so if you've tried this then something else is off, and watching the generated SQL will begin to describe what's actually happening. -- 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.