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条)

报告相同问题?

悬赏问题

  • ¥20 iOS绕地区网络检测
  • ¥15 python验证码滑块图像识别
  • ¥15 根据背景及设计要求撰写设计报告
  • ¥15 QT6颜色选择对话框显示不完整
  • ¥20 能提供一下思路或者代码吗
  • ¥15 用twincat控制!
  • ¥15 请问一下这个运行结果是怎么来的
  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥15 DS18B20内部ADC模数转换器