postgresql 从 Django 过滤器调用返回的列表的默认顺序是什么?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7163640/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-20 23:14:22  来源:igfitidea点击:

What is the default order of a list returned from a Django filter call?

pythondjangopostgresqlsql-order-bydjango-orm

提问by Adam Lewis

Short Question
What is the default order of a list returned from a Django filter call when connected to a PostgreSQL database?

简短问题
当连接到 PostgreSQL 数据库时,从 Django 过滤器调用返回的列表的默认顺序是什么?

Background
By my own admission, I hadmade a poor assumption at the application layer in that the order in which a list is returned will be constant, that is without using 'order_by'. The list of items I was querying is not in alphabetic order or any other deliberateorder. It was thought to remain in the same order as which they were added to the database.

背景
通过我自己也承认,我已经在做应用层一个糟糕的假设,其中返回的列表将是恒定的秩序,对不使用“ORDER_BY”。我查询的项目列表不是按字母顺序或任何其他故意顺序排列的。它被认为与它们被添加到数据库中的顺序相同。

This assumption held true for hundreds of queries, but a failure was reported by my application when the order changed unknowingly. To my knowledge, none of these records were touched during this time as I am the only person who maintains the DB. To add to the confusion, when running the Django app on Mac OS X, it still worked as expected, but on Win XP, it changed the order. (Note that the mentioned hundreds of queries was on Win XP).

这个假设适用于数百个查询,但是当订单在不知不觉中发生变化时,我的应用程序报告了一个失败。据我所知,在此期间这些记录都没有被触及,因为我是唯一维护数据库的人。更令人困惑的是,在 Mac OS X 上运行 Django 应用程序时,它仍然按预期工作,但在 Win XP 上,它更改了顺序。(请注意,提到的数百个查询是在 Win XP 上进行的)。

Any insight to this would be helpful as I could not find anything in the Django or PostgreSQL documentation that explained the differences in operating systems.

对此的任何见解都会有所帮助,因为我在 Django 或 PostgreSQL 文档中找不到任何解释操作系统差异的内容。

Example Call

示例调用

required_tests = Card_Test.objects.using(get_database()).filter(name__icontains=key)

EDIT
After speaking with some colleague's of mine today, I had come up with the same answer as Bj?rn Lindqvist.

编辑
今天与我的一些同事交谈后,我得出了与 Bj?rn Lindqvist 相同的答案。

Looking back, I definitely understand why this is done wrong so often. One of the benefits to using an ORM Django, sqlalchemy, or whatever is that you can write commands without having to know or understand (in detail) the database it's connected to. Admittedly I happen to have been one of these users. However on the flip-side of this is that without knowing the database in detail debugging errors like this are quite troublesome and potentially catastrophic.

回想起来,我绝对明白为什么这经常做错。使用 ORM Django、sqlalchemy 或其他任何东西的好处之一是您可以编写命令而无需知道或了解(详细)它所连接的数据库。诚然,我碰巧是这些用户之一。然而,另一方面是,在不详细了解数据库的情况下,调试像这样的错误非常麻烦并且可能是灾难性的。

回答by Bj?rn Lindqvist

There is NO DEFAULT ORDER, a point that can not be emphasized enough because everyone does it wrong.

否默认顺序,不能强调不够,因为每个人都没有错点。

A table in a database is not an ordinary html table, it is an unordered set of tuples. It often surprises programmers only used to MySQL because in that particular database the order of the rows are often predictable due to it not taking advantage of some advanced optimization techniques. For example, it is not possible to know which rows will be returned, or their order in any of the following queries:

数据库中的表不是普通的 html 表,它是一组无序的元组。它常常让只使用 MySQL 的程序员感到惊讶,因为在那个特定的数据库中,行的顺序通常是可预测的,因为它没有利用一些高级优化技术。例如,无法知道将返回哪些行,或者它们在以下任何查询中的顺序:

select * from table limit 10
select * from table limit 10 offset 10
select * from table order by x limit 10

In the last query, the order is only predictable if all values in column x are unique. The RDBMS is free to returns any rows in any order it pleases as long as it satisfies the conditions of the select statement.

在最后一个查询中,只有当 x 列中的所有值都是唯一的时,顺序才是可预测的。只要 RDBMS 满足 select 语句的条件,它就可以自由地以它喜欢的任何顺序返回任何行。

Though you may add a default ordering on the Django level, which causes it to add an order by clause to every non-ordered query:

尽管您可以在 Django 级别添加默认排序,这会导致它向每个非有序查询添加 order by 子句:

class Table(models.Model):
    ...
    class Meta:
        ordering = ['name']

Note that it may be a performance drag, if for some reason you don't need ordered rows.

请注意,如果由于某种原因您不需要有序行,这可能会拖累性能。

回答by Risadinha

If you want to have them returned in the order they were inserted:

如果您想让它们按照插入的顺序返回:

Add the following to your model:

将以下内容添加到您的模型中:

created = models.DateTimeField(auto_now_add=True, db_index=True)
# last_modified = models.DateTimeField(auto_now=True, db_index=True)

class Meta:
    ordering = ['created',]
    # ordering = ['-last_modified']  # sort last modified first