这个问题可能是我不了解(新)sqlalchemy的体系结构,通常我使用这样的代码:
query = select(models.Organization).where(
models.Organization.organization_id == organization_id
)
result = await self.session.execute(query)
return result.scalars().all()
工作正常,我会得到模型列表(如果有)。
只有带有特定列的查询:
query = (
select(
models.Payment.organization_id,
models.Payment.id,
models.Payment.payment_type,
)
.where(
models.Payment.is_cleared.is_(True),
)
.limit(10)
)
result = await self.session.execute(query)
return result.scalars().all()
我只获得第一行,仅第一列。似乎也一样:
到目前为止,我的理解是,在新的SQLalchemy中,我们应该始终致电 scalars scalars ()
在查询上,如下所述: https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#migration-migration-orm-usage
但是,对于特定的列,似乎我们完全不能使用标量()。更令人困惑的是 result.scalars()
返回 sqlalchemy.engine.result.scalarresult
具有fetchmany()以任何有意义的方式迭代。
我的问题是,我不明白什么?
This question is probably me not understanding architecture of (new) sqlalchemy, typically I use code like this:
query = select(models.Organization).where(
models.Organization.organization_id == organization_id
)
result = await self.session.execute(query)
return result.scalars().all()
Works fine, I get a list of models (if any).
With a query with specific columns only:
query = (
select(
models.Payment.organization_id,
models.Payment.id,
models.Payment.payment_type,
)
.where(
models.Payment.is_cleared.is_(True),
)
.limit(10)
)
result = await self.session.execute(query)
return result.scalars().all()
I am getting first row, first column only. Same it seems to: https://docs.sqlalchemy.org/en/14/core/connections.html?highlight=scalar#sqlalchemy.engine.Result.scalar
My understanding so far was that in new sqlalchemy we should always call scalars()
on the query, as described here: https://docs.sqlalchemy.org/en/14/changelog/migration_20.html#migration-orm-usage
But with specific columns, it seems we cannot use scalars() at all. What is even more confusing is that result.scalars()
returns sqlalchemy.engine.result.ScalarResult
that has fetchmany(), fechall() among other methods that I am unable to iterate in any meaningful way.
My question is, what do I not understand?
发布评论
评论(1)
,这主要是正确的,但仅用于返回整个ORM对象的查询。只有一个常规
.execute()
返回
行
对象的列表,每个列表包含一个ORM对象。用户发现尴尬,因为他们需要从row
对象解开ORM对象。因此,现在建议使用.scalars()
,对于返回单个属性(列)的查询,我们不想使用
.scalars()
,因为这只会给我们每行的一列,通常是第一列,我们要使用常规
.execute()
,以便我们可以看到所有列注:
.scalars()正在执行相同的事情:返回每行中包含一个(标量)值的列表(默认为index = 0)。
sess.scalars()
是首选构造。它只是sess.execute()。标量()
。的速记
That is mostly true, but only for queries that return whole ORM objects. Just a regular
.execute()
returns a list of
Row
objects, each containing a single ORM object. Users found that awkward since they needed to unpack the ORM object from theRow
object. So.scalars()
is now recommendedHowever, for queries that return individual attributes (columns) we don't want to use
.scalars()
because that will just give us one column from each row, normally the first columnInstead, we want to use a regular
.execute()
so we can see all the columnsNotes:
.scalars()
is doing the same thing in both cases: return a list containing a single (scalar) value from each row (default is index=0).sess.scalars()
is the preferred construct. It is simply shorthand forsess.execute().scalars()
.