On 02/13/2016 12:00 PM, [email protected] wrote:
I create a many-to-many relationship between a `Person` (as an
_author_) and a `Reference` (as a _scientific publication_).

ReferenceAuthor = sa.Table('ReferenceAuthor', _Base.metadata,
         sa.Column('ReferenceID',  sa.Integer,
sa.ForeignKey('Reference.ID'), primary_key=True), sa.Column('PersonID',
sa.Integer, sa.ForeignKey('Person.ID'), primary_key=True),
sa.Column('Index', sa.Integer) )


class Reference(_Base):
     __tablename__ = 'Reference'

     _id = sa.Column('ID', sa.Integer, primary_key=True)

     _authors = sao.relationship('Person', secondary=ReferenceAuthor)


class Person(_Base):
     __tablename__ = 'Person'

     _id = sa.Column('ID', sa.Integer, primary_key=True)

I would like to have the objects in `Reference().authors` ordered by
`ReferenceAuthor.Index`. The three tables here come from a foreign
sqlite3-database. I am not able to change the structure of that tables.
I have to deal with them like they are. I could finde some examples
about `order_by` in `relationship()` but nothing combined with the use
of `secondary=`.


you can order by secondary, just add it in:

authors = relationship("Person", secondary=ReferenceAuthor, order_by=ReferenceAuthor.c.Index)




I get the hint on stackoverflow [1] to use association_proxy. But I can
get it work.

[1]
<https://stackoverflow.com/questions/35375846/how-to-use-order-by-in-sqlalchemy-when-using-relationship-with-secondary-param>


--
You received this message because you are subscribed to the Google Groups 
"sqlalchemy" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at https://groups.google.com/group/sqlalchemy.
For more options, visit https://groups.google.com/d/optout.

Reply via email to