q = session.query(Foo).options(sa.orm.joinedload('bars')) q = q.order_by(q.with_labels().statement.c.foo_name)
On 05/24/2016 05:26 AM, Andrew Pashkin wrote:
Here is the working example: import sqlalchemy as sa from sqlalchemy.ext.declarative import declarative_base engine = sa.create_engine('sqlite:///:memory:', echo=True) Base = declarative_base() class Foo(Base): __tablename__ = 'foo' id = sa.Column(sa.Integer, primary_key=True) name = sa.Column(sa.String) def __repr__(self): return 'Foo(id=%s, name="%s")' % (self.id, self.name) class Bar(Base): __tablename__ = 'bar' id = sa.Column(sa.Integer, primary_key=True) name = sa.Column(sa.String) foo_id = sa.Column(sa.Integer, sa.ForeignKey('foo.id'), nullable=False) foo = sa.orm.relationship('Foo', backref='bars') Base.metadata.create_all(engine) Session = sa.orm.sessionmaker(bind=engine) session = Session() baz = Foo(name='baz') qux = Foo(name='qux') session.add(baz) session.add(qux) session.add(Bar(name='spam', foo=baz)) session.add(Bar(name='parrot', foo=baz)) session.commit() q = session.query(Foo).options(sa.orm.joinedload('bars')) q = q.order_by(q.statement.c.name) # q = q.order_by(Foo.name) # Doesn't give an error print(q.all()) # Gives: # # sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) # ambiguous column name: name Here I have a two tables with columns name and one references another with FK. When they are joined - name becomes ambiguous, and SQLAlchemy ORM resolves this collision automatically. But how to do the same in case of using SQLAlchemy expressions? -- With kind regards, Andrew Pashkin. cell phone - +7 (985) 898 57 59 Skype - waves_in_fluids e-mail - andrew.pash...@gmx.co.uk -- 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 sqlalchemy+unsubscr...@googlegroups.com <mailto:sqlalchemy+unsubscr...@googlegroups.com>. To post to this group, send email to sqlalchemy@googlegroups.com <mailto:sqlalchemy@googlegroups.com>. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.
-- 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 sqlalchemy+unsubscr...@googlegroups.com. To post to this group, send email to sqlalchemy@googlegroups.com. Visit this group at https://groups.google.com/group/sqlalchemy. For more options, visit https://groups.google.com/d/optout.