douyan8267 2016-08-08 12:38
浏览 21
已采纳

带有过滤器的AppEngine数据存储区查询从不返回结果(Go)

If I don't use a filter, I get results. If I use a filter (this record definitely exists), I don't get any results. It might be the lack of an index defined for that property, but, as I understand it, simple indices should automatically be created in the development server (and an index.yaml file created and populated with it). This is not happening.

query = datastore.NewQuery("UserAccount").Filter("email =", "test@example.com")

ua := UserAccount{}
t := query.Run(ctx)
for ; ; {
    if _, err = t.Next(&ua); err == nil {
        log.Debugf(ctx, "Current: %s", ua)
    } else if err == datastore.Done {
        break
    } else {
        panic(err)
    }
}

When the development server terminates, it states that it's "saving search indexes":

INFO     2016-08-08 05:09:52,894 api_server.py:651] Saving search indexes

However, since an "index.yaml" file doesn't appear, I'm assuming that no indices needed to be created, which means that my query must not've had the desired effect?

What am I missing?

Edit:

Note that the record was previously created and the application stopped and started many times since. I sincerely doubt this is a eventual-consistency thing.

Edit 2:

For the purpose of testing, I've created the following model with the following code. They both exhibit the same behavior as my original model and code.

Definition:

type TestEntity struct {
    Email string
}

Code:

log.Debugf(ctx, "Putting.")

email := "anothertest@a.b"

te := &TestEntity{
        Email: email,
}

k := datastore.NewKey(ctx, "TestEntity", "123", 0, nil)
_, err = datastore.Put(ctx, k, te)
if err != nil {
    panic(err)
}

log.Debugf(ctx, "Waiting.")
time.Sleep(1 * time.Second)

query := datastore.NewQuery("TestEntity")

var results []TestEntity
_, err = query.GetAll(ctx, &results)
log.Debugf(ctx, "GetAll: %s", results)

log.Debugf(ctx, "Running query.")

query = datastore.NewQuery("TestEntity").Filter("email =", email)

te = &TestEntity{}
t := query.Run(ctx)
for ; ; {
    if _, err = t.Next(te); err == nil {
        log.Debugf(ctx, "Found: [%s]", te.Email)
    } else if err == datastore.Done {
        log.Debugf(ctx, "Done.")
        break
    } else {
        panic(err)
    }
}

Results:

2016/08/09 02:11:36 DEBUG: Putting.
2016/08/09 02:11:36 DEBUG: Waiting.
2016/08/09 02:11:37 DEBUG: GetAll: [{anothertest@a.b}]
2016/08/09 02:11:37 DEBUG: Running query.
2016/08/09 02:11:37 DEBUG: Done.

Screenshot of Viewer:

Screenshot of Viewer

  • 写回答

2条回答 默认 最新

  • douchi7073 2016-08-09 05:41
    关注

    The property name in the datastore is "Email" with capital E, not "email".

    It's case-sensitive, must be queried with capital E:

    query = datastore.NewQuery("UserAccount").Filter("Email =", "test@example.com")
    

    If you would want it to be saved / retrieved with small e, you could use tags to do the mapping, e.g.:

    type UserAccount struct {
        Email string `datastore:"email"`
        // other fields...
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计