4 messages in com.googlegroups.sqlalchemy[sqlalchemy] Re: "order_by" computed ...
FromSent OnAttachments
Alexandre CONRAD30 Mar 2007 02:50 
Michael Bayer30 Mar 2007 08:16 
Alexandre CONRAD02 Apr 2007 03:28 
Michael Bayer02 Apr 2007 09:11 
Subject:[sqlalchemy] Re: "order_by" computed on wrong table with many-to-many
From:Michael Bayer (zzz@gmail.com)
Date:03/30/2007 08:16:23 AM
List:com.googlegroups.sqlalchemy

use 'sites':relation(Site, backref=backref('attachments', order_by=attachment_table.c.name)) for now.

On Mar 30, 5:50 am, Alexandre CONRAD <acon@magic.fr> wrote:

Hello,

I have a problem with "order_by" on a many-to-many relationship (using assign_mapper):

------------------------------ attachment_table = Table('attachments', meta, Column('id', Integer, primary_key=True), Column('file', Binary, nullable=False), Column('name', Unicode(40), nullable=False), Column('type', Unicode(30)), Column('date', DateTime, default=datetime.now), Column('size', Integer, nullable=False), Column('description', Unicode(40)), )

attachments_has_sites = Table('attachments_has_sites', meta, Column('id_attachment', None, ForeignKey('attachments.id'), primary_key=True), Column('id_site', None, ForeignKey('sites.id'), primary_key=True), )

class Attachment(object): pass

attachment_mapper = assign_mapper(ctx, Attachment, attachment_table, properties={ 'file':deferred(attachment_table.c.file), 'sites':relation(Site, backref="attachments", secondary=attachments_has_sites, cascade="save-update"), }, order_by=attachment_table.c.name, )

------------------------------

So I have a Site object where I can ask for it's attachments:

s = model.Site.get(1) attachment_list = s.attachments

But it fires the following QUERY:

SELECT attachments.name AS attachments_name, attachments.description AS attachments_description, attachments.date AS attachments_date, attachments.type AS attachments_type, attachments.id AS attachments_id, attachments.size AS attachments_size FROM attachments, attachments_has_sites WHERE %s = attachments_has_sites.id_site AND attachments.id = attachments_has_sites.id_attachment ORDER BY attachments_has_sites.id_attachment

the "ORDER BY" is computed against the weak table (secondary) "attachments_has_sites".

Regards,