如果我有Order子句,Google App Engine Datastore不会返回任何行 [英] Google App Engine Datastore returns no rows if i have an Order clause

查看:134
本文介绍了如果我有Order子句,Google App Engine Datastore不会返回任何行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据存储中有这样一种'kind':

  type CompanyDS struct {
名称字符串



$ b $ p
$ b如果我用下面的'order'子句查询它,它不返回任何行(但是不会给出任何错误):

  var companiesDS [] CompanyDS 
datastore.NewQuery(Company)。 Order(Name)。GetAll(c,& companiesDS)

然而,如果我删除' order(Name)'section它会返回所有行。

解决方案

由于没有 Order ()您可以查询所有实体,这意味着它们确实存在名称公司和属性名称



自动创建单个属性的索引,因此您无需为它们指定显式索引。



但是,如果您不能使用像 Order(Name)这样的单个属性列出它们,那意味着您的现有实体未使用名称属性编入索引。请注意,每个实体的索引都可能不同。将实体保存(放入)到数据存储中时,您可以指定哪些属性将被索引,哪些不是。



您可以在Google云端平台数据存储控制台:执行查询

  select * from Company 

然后点击任何结果(它的ID),然后你会看到该实体的细节,列出哪些属性被索引,哪些不是。您可以在控制台上编辑实体:点击名称属性,然后在保存之前,请检查索引此属性。这将重新保存这个实体,使它的 Name 索引,因此它会显示在下一个查询中(按 Name

您不需要为所有实体手动执行此操作。使用你的Go查询代码(不包含 Order()),查询所有实体,然后不加修改地重新保存所有实体,所以 Name CompanyDS 没有关闭名称的索引,所以c $ c>属性)。确保你的结构体包含所有的属性,否则在重新保存时你会失去它们。



注意: 公司实体将它们保存为名称索引。



以例如结构标记为例,noindex将禁用像下面这样的单个属性的索引:

  type CompanyDS struct {
名称字符串`datastore:,noindex`
}


I have a 'kind' in datastore like so:

type CompanyDS struct {
    Name string
}

If i query it with the 'order' clause below, it returns no rows (but doesn't give any error):

var companiesDS []CompanyDS
datastore.NewQuery("Company").Order("Name").GetAll(c, &companiesDS)

However if i remove the 'order("Name")' section it returns all the rows just fine.

解决方案

Since without Order() you can query all entities, that means they do exist with name "Company" and property "Name".

Indices for single properties are automatically created, so you don't need to specify explicit index for them.

But if you can't list them using a single property ordering like Order("Name"), that means that your existing entities are not indexed with the Name property. Note that every single entity may be indexed differently. When you save (put) an entity into the Datastore, you have the ability to specify which properties are to be indexed and which are not.

You can confirm this on the Google Cloud Platform Datastore console: execute the query

select * from Company

Then click on any of the results (its ID), then you will see the details of that entity, listing which property is indexed and which is not.

Fix:

You may edit the entities on the console: click on the "Name" property, and before saving, check the "Index this property". This will re-save this entity, making its Name indexed, and thus it will show up in the next query (ordered by Name).

You don't need to do this manually for all entities. Use your Go query code (without Order()), query all entities, then re-save all without modification, and so the Name will get indexed as a result of this (because your CompanyDS does not turn off indexing for the Name property). Make sure your struct contains all properties, else you would lose them when re-saving.

Note: You should ensure that the code that saves Company entities saves them with Name indexed.

In Go for example a struct tag with value ",noindex" will disable indexing for a single property like in this example:

type CompanyDS struct {
    Name string `datastore:",noindex"`
}

这篇关于如果我有Order子句,Google App Engine Datastore不会返回任何行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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