SQL Alchemy ORM 返回单列,如何避免常见的后处理 [英] SQL Alchemy ORM returning a single column, how to avoid common post processing

查看:13
本文介绍了SQL Alchemy ORM 返回单列,如何避免常见的后处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 SQL Alchemy 的 ORM,当我返回单个列时,我发现结果如下:

I'm using SQL Alchemy's ORM and I find when I return a single column I get the results like so:

[(result,), (result_2,)] # etc...

有了这样的一套,我发现我必须经常这样做:

With a set like this I find that I have to do this often:

results = [r[0] for r in results] # So that I just have a list of result values

这并不是那么糟糕",因为我的结果集通常很小,但如果不是,这可能会增加大量开销.最重要的是我觉得它使源代码变得混乱,而错过这一步是我遇到的一个非常常见的错误.

This isn't that "bad" because my result sets are usually small, but if they weren't this could add significant overhead. The biggest thing is I feel it clutters the source, and missing this step is a pretty common error I run into.

有什么办法可以避免这个额外的步骤吗?

Is there any way to avoid this extra step?

一个相关的旁白:在这种情况下,orm 的这种行为似乎很不方便,但在另一种情况下,我的结果集是 [(id, value)] 它最终是这样的:

A related aside: This behaviour of the orm seems inconvenient in this case, but another case where my result set was, [(id, value)] it ends up like this:

[(result_1_id, result_1_val), (result_2_id, result_2_val)]

然后我可以这样做:

results = dict(results) # so I have a map of id to value

此方法的优点是作为返回结果后的有用步骤有意义.

This one has the advantage of making sense as a useful step after returning the results.

这真的是个问题还是我只是吹毛求疵,获得结果集后的后期处理对这两种情况都有意义?我相信我们可以想到一些其他常见的后处理操作,以使结果集在应用程序代码中更有用.是否有全面的高性能和方便的解决方案,或者后期处理是不可避免的,并且只是为了不同的应用程序使用而需要?

Is this really a problem or am I just being a nitpick and the post processing after getting the result set makes sense for both cases? I'm sure we can think of some other common post processing operations to make the result set more usable in the application code. Is there high performance and convenient solutions across the board or is post processing unavoidable, and merely required for varying application usages?

当我的应用程序实际上可以利用 SQL Alchemy 的 ORM 返回的对象时,这似乎非常有用,但在我不能或不可以的情况下,就没有那么多了.这只是 ORM 的普遍问题吗?在这种情况下,我最好不要使用 ORM 层吗?

When my application can actually take advantage of the objects that are returned by SQL Alchemy's ORM it seems extremely helpful, but in cases where I can't or don't, not so much. Is this just a common problem of ORMs in general? Am I better off not using the ORM layer in cases like this?

我想我应该展示一个我正在谈论的实际 orm 查询的例子:

I suppose I should show an example of the actual orm queries I'm talking about:

session.query(OrmObj.column_name).all()

session.query(OrmObj.id_column_name, OrmObj.value_column_name).all()

当然,在实际查询中通常会有一些过滤器等.

Of course, in a real query there'd normally be some filters, etc.

推荐答案

减少源代码混乱的一种方法是像这样迭代:

One way to decrease the clutter in the source is to iterate like this:

results = [r for (r, ) in results]

虽然这个解决方案比使用 [] 运算符长一个字符,但我认为它更容易理解.

Although this solution is one character longer than using the [] operator, I think it's easier on the eyes.

为了减少混乱,请删除括号.这使得在阅读代码时更难注意到您实际上正在处理元组:

For even less clutter, remove the parenthesis. This makes it harder when reading the code, to notice that you're actually handling tuples, though:

results = [r for r, in results]

这篇关于SQL Alchemy ORM 返回单列,如何避免常见的后处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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