【发布时间】:2016-04-23 09:54:18
【问题描述】:
对于我们正在构建的 Web 应用程序,我们使用 SQLite 进行测试。最近我们想迁移到 PostgreSQL。这就是问题开始的地方:
我们有这个 SQLAlchemy 模型(简化)
class Entity(db.Model):
id = db.Column(db.Integer, primary_key=True)
i_want_this = db.Column(db.String)
some_value = db.Column(db.Integer)
我想将所有Entitys 按some_value 分组,我这样做了(简化):
db.session.query(Entity, db.func.count()).group_by(Entity.some_value)
在 SQLite 中这行得通。回想起来,我发现它没有意义,但 SQLite 确实有意义。我无法确定返回了哪些实体。
现在在 PostgrSQL 中我们得到这个错误:
sqlalchemy.exc.ProgrammingError: (psycopg2.ProgrammingError) column "entity.id" must appear in the GROUP BY clause or be used in an aggregate function
LINE 1: SELECT entity.id AS entity_id, entity.i_want_this AS entity_not...
^
[SQL: 'SELECT entity.id AS entity_id, entity.i_want_this AS entity_i_want_this, count(*) AS count_1 \nFROM entity GROUP BY entity.some_value']
这个错误完全有道理。
所以我的第一个问题是:为什么 SQLite 允许这样做以及它是如何做到的(使用什么隐藏聚合)?
我的第二个问题很明显:我将如何使用 PostgreSQL?
我实际上只对计数和第一个 i_want_this 值感兴趣。所以我可以这样做:
groups = db.session.query(db.func.min(Entity.id), db.func.count()).group_by(Entity.some_value)
[(Entity.query.get(id_), count) for id_, count in groups]
但我不想要这些额外的get 查询。
所以我想选择第一个实体(具有最小 id 的实体)和由some_value 或第一个i_want_this 分组的实体数和由some_value 分组的计数
编辑以明确:
- 我想按
some_value分组(完成) - 我想获取每个组中的实体数量(完成)
- 我想获得每个组中
id最低的实体(需要帮助) -
或者我想获取每个组中
id最低的实体的i_want_this值(需要帮助)
【问题讨论】:
-
SQLite 从组中的随机行返回一个值。但是“第一”是什么意思? SQL 表未排序。
-
我的意思是在我上一个代码 sn-p 中看到的具有最低 id 的实体。
标签: python postgresql sqlite group-by sqlalchemy