dongren4099 2017-08-22 14:43
浏览 11
已采纳

Google数据存储中的唯一电子邮件

I have a User entity containing an Email field. The User entity id is a ULID, because I want to allow users to change their email addresses, but I want to ensure that the email address is unique on both a CREATE and an UPDATE.

I am using Datastore transactions. This is a code fragment:

ctx := context.Background()
k := datastore.NameKey("User", user.ID, nil)
_, err := client.RunInTransaction(ctx, func(t *datastore.Transaction) error {
    // other stuff that needs to be in transaction
    _, err = t.Put(k, user)
    return err
})

return err

The Email field is indexed. Is there any way to search the User entity for the current user's email address as part of the transaction?

*datastore.Transaction does not have a GetAll method, so I cannot run a query like this:

datastore.NewQuery("User").Filter("Email =", user.Email)

I'm afraid that using

client.GetAll(ctx, q, nil)

will not guarantee isolation within the transaction.

  • 写回答

1条回答 默认 最新

  • dream5694 2017-08-22 15:12
    关注

    The short answer is no, you cannot use a query as part of a transaction unless you are querying a specific entity group. Global queries are alway eventually consistent. However, to put everything in a single entity group would likely limit write throughput too much.

    A workaround is you can have another Kind with entities that map email addresses to users. Then you can, in a transaction, check the email Entity and if it doesn't exist or it points to a bad location, set the email Entity and the user Entity all as a single transaction.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 有偿求数字经济对经贸的影响机制的一个数学模型,弄不出来已经快要碎掉了
  • ¥15 这个公式写进SIMULINK中的function模块的代码中应该是什么样的
  • ¥15 javaweb登陆的网页为什么不能正确连接查询数据库
  • ¥15 数学建模数学建模需要
  • ¥15 已知许多点位,想通过高斯分布来随机选择固定数量的点位怎么改
  • ¥20 nao机器人语音识别问题
  • ¥15 怎么生成确定数目的泊松点过程
  • ¥15 layui数据表格多次重载的数据覆盖问题
  • ¥15 python点云生成mesh精度不够怎么办
  • ¥15 QT C++ 鼠标键盘通信