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

查看:89
本文介绍了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天全站免登陆