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=
.
I tried an association_proxy
and read the docs about that.
class ReferenceAuthor(_Base):
__tablename__ = 'ReferenceAuthor'
_reference_id = sa.Column('ReferenceID', sa.Integer, sa.ForeignKey('Reference.ID'), primary_key=True)
_person_id = sa.Column('PersonID', sa.Integer, sa.ForeignKey('Person.ID'), primary_key=True)
_index = sa.Column('Index', sa.Integer)
_person = sao.relationship('Person')
class Reference(_Base):
__tablename__ = 'Reference'
_id = sa.Column('ID', sa.Integer, primary_key=True)
_authors = saa.association_proxy('ReferenceAuthor', 'Person')
When I read the docs I think I understand the concept behind it. But with my code it doesn't work and I don't understand why. The error is
sqlalchemy.exc.InvalidRequestError: Mapper 'Mapper|Reference|Reference' has no property 'ReferenceAuthor'
.