Why properties referenced in an equality (EQUAL) or membership (IN) filter cannot be projected?

a 夏天 提交于 2020-01-16 18:52:09

问题


https://developers.google.com/appengine/docs/java/datastore/projectionqueries

Why a projected query such as this : SELECT A FROM kind WHERE A = 1 not supported ?


回答1:


Because it makes no sense. You are asking

SELECT A FROM kind WHERE A = 1

so, give me A where A = 1. Well, you already know that A = 1. It makes no sense for DB to allow that.

The IN query is internally just a series of equals queries merged together, so the same logic applies to it.




回答2:


The reasoning behind this could be that since you already have the values of the properties you are querying you don't need them returned by the query. This is probably a good thing in the long run, but honestly, it's something that App Engine should allow anyway. Even if it didn't actually fetch these values from the datastore, it should add them to the entities returned to you behind the scenes so you can go about your business.

Anyway, here's what you can do...

query = MyModel.query().filter(MyModel.prop1 == 'value1', MyModel.prop2 == 'value2)
results = query.fetch(projection=[MyModel.prop3])
for r in results:
  r.prop1 = 'value1'  # the value you KNOW is correct
  r.prop2 = 'value2'

Again, would be nice for this to happen behind the scenes because I don't think it's something anybody should ever care about. If I mention a property in a projection list, I'm already stating that I want that property as part of my entities. I shouldn't have to do any more computation to get that to happen.

On the other hand, it's just an extra for-loop. :)



来源:https://stackoverflow.com/questions/15714386/why-properties-referenced-in-an-equality-equal-or-membership-in-filter-canno

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!