Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/279.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何替换SQLAlchemy mapper已弃用“;订购人;选项_Python_Python 3.x_Sqlalchemy - Fatal编程技术网

Python 如何替换SQLAlchemy mapper已弃用“;订购人;选项

Python 如何替换SQLAlchemy mapper已弃用“;订购人;选项,python,python-3.x,sqlalchemy,Python,Python 3.x,Sqlalchemy,我有一个单表继承sqlalchemy orm模型,看起来像这样: class人员(基本): __tablename_=“人类” id=列(整数,主键=True) 名称=列(字符串) 索引=列(整数) parent_id=Column(整数,ForeignKey(“human.id”),nullable=True) 类型=列(字符串) 父=关系( “人类”, cascade=“保存更新,合并”, backref=backref(“子项”,cascade=“全部”), 懒惰=错误, remote_s

我有一个
单表继承
sqlalchemy orm模型,看起来像这样:

class人员(基本):
__tablename_=“人类”
id=列(整数,主键=True)
名称=列(字符串)
索引=列(整数)
parent_id=Column(整数,ForeignKey(“human.id”),nullable=True)
类型=列(字符串)
父=关系(
“人类”,
cascade=“保存更新,合并”,
backref=backref(“子项”,cascade=“全部”),
懒惰=错误,
remote_side=“Human.id”,
)
__mapper_args_uuu={“多态性”:类型,“多态性身份”:“人类”}
班级家长(人):
__mapper_args_uu={“多态性_标识”:“父”}
班级儿童(人类):
爱好=列(字符串)
pet=列(字符串)
__mapper_args_uu={“多态性_标识”:“子”}
这个关系看起来像一个嵌套的树,在这里我可以查询最年长的人(即:曾祖父母,使用
query.get(1)
),然后在他的
children
属性中,他的所有
孩子都会出现,每个孩子也会有他们的
孩子出现。
这种关系在各个方面都很好

我希望查询父项时查询的子项(由于lazy=False)按
索引
列排序,而不是按
id
列排序(默认)

为此,我在
人类
类的
\uuu映射器u参数u
中添加了以下
“order u by”:“index”
,如下所示:

\uuuuu映射器\u参数\uuuuuuu={“多态性”:类型,“多态性”;“身份”:“人类”,“排序依据”:“索引”}
我最近发现在SQLAlchemy中,
mapper()
类中的
order\u by
参数是不推荐的,因为SQLAlchemy版本
1.1
()。他们说使用
Query.order_by()
来对集合进行排序,但出于某种原因,这对我来说不起作用

我尝试了以下方法:

  • order\u by
    添加到查询中,
    session.query(Human).order\u by(Human.index).get(1)
  • order\u by=“Human.index”
    添加到
    Human
    类中的
    relationship()
这两种方法都不起作用


既然
mapper()
中的
order\u被弃用了,我如何才能达到与
mapper()中的
order\u相同的效果呢?

我在问了一个问题后得到了答案。 我会做出回应,以防其他人搜索这个问题并在这里找到它,而不是sqlalchemy的github

关系加载的集合的顺序由relationship()和/或backref的order_by参数控制。在这种情况下,您希望对“children”进行排序,因此将其放在定义“children”的位置:

parent=关系(
“人类”,
cascade=“保存更新,合并”,
backref=backref(“children”,cascade=“all”,order_by=“Human.index”),
懒惰=错误,
remote_side=“Human.id”,
)
也就是说,这与mapper.order_by无关,尽管该属性具有您想要的效果。如果您在“父”端尝试了relationship->order__by,那是错误的,它会转到定义实际要订购的东西的地方,在本例中是backref


还在GitHub上讨论。@GordThompson,我也在那里发布了它:)