SQLAlchemy:如何对关系字段的查询结果 (order_by) 进行排序? [英] SQLAlchemy: How to order query results (order_by) on a relationship's field?

查看:33
本文介绍了SQLAlchemy:如何对关系字段的查询结果 (order_by) 进行排序?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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))

正在搜索

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.

它总是导致:

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

这一定很容易……但我不明白.还研究了比较器,这似乎合乎逻辑,但我没有看到 ORDER BY 的查询部分是在哪里生成的,或者我应该返回什么,因为一切都是动态生成的.为我的每个玩家"关系制作一个比较器来做一件简单的事情似乎过于复杂.

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.

推荐答案

SQLAlchemy 希望您从 SQL 的角度思考.如果您查询Base",那就是:

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

SELECT * FROM base

容易.那么,在 SQL 中,如何从base"中选择行并按name"列在一个完全不同的表(即player")中排序?您使用连接:

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 有你使用相同的思维过程 - 你 join():

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

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

这篇关于SQLAlchemy:如何对关系字段的查询结果 (order_by) 进行排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆