Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
602 views
in Technique[技术] by (71.8m points)

python - SQLAlchemy: How to order query results (order_by) on a relationship's field?

Models

from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, ForeignKey
from sqlalchemy import Integer
from sqlalchemy import Unicode
from sqlalchemy import TIMESTAMP
from sqlalchemy.orm import relationship

BaseModel = declarative_base()

class Base(BaseModel):
   __tablename__ = 'base'
   id = Column(Integer, primary_key=True)
   location = Column(Unicode(12), ForeignKey("locationterrain.location"), unique=True,)
   name = Column(Unicode(45))
   ownerid =  Column(Integer,ForeignKey("player.id"))
   occupierid =  Column(Integer, ForeignKey("player.id"))
   submitid =  Column(Integer,ForeignKey("player.id"))
   updateid =  Column(Integer,ForeignKey("player.id"))
   owner = relationship("Player",
         primaryjoin='Base.ownerid==Player.id',
         join_depth=3,
         lazy='joined')
   occupier= relationship("Player",
         primaryjoin='Base.occupierid==Player.id',
         join_depth=3,
         lazy='joined')
   submitter = relationship("Player",
         primaryjoin='Base.submitid== Player.id',
         join_depth=3,
         lazy='joined')
   updater= relationship("Player",
         primaryjoin='Base.updateid== Player.id',
         join_depth=3,
         lazy='joined')


class Player(BaseModel):
   __tablename__ = 'player'
   id = Column(Integer, ForeignKey("guildmember.playerid"), primary_key=True)
   name =  Column(Unicode(45))

Searching

bases = dbsession.query(Base)
bases = bases.order_by(Base.owner.name)

This doesn't work .... I've searched everywhere and read the documentation. But I just don't see how I can sort my (Base) query on their 'owner' relationship's name.

It always results in:

 AttributeError: Neither 'InstrumentedAttribute' object nor 'Comparator' object has an attribute 'name'

This must be easy... but I don't see it. Also looked into Comparators, which seemed logical, but I don't see where the query part for the ORDER BY is generated or what I should be returning since everything is generated dynamically. And making a comparator for each of my 'player' relationships to do a simple thing seems over complicated.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

SQLAlchemy wants you to think in terms of SQL. If you do a query for "Base", that's:

SELECT * FROM base

easy. So how, in SQL, would you select the rows from "base" and order by the "name" column in a totally different table, that is, "player"? You use a join:

SELECT base.* FROM base JOIN player ON base.ownerid=player.id ORDER BY player.name

SQLAlchemy has you use the identical thought process - you join():

session.query(Base).join(Base.owner).order_by(Player.name)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...