修改Google App Engine实体ID? [英] Modify a Google App Engine entity id?

查看:128
本文介绍了修改Google App Engine实体ID?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Google App Engine NDB。有时候我会想让所有用户在指定的列表中输入一个电话号码。使用查询对此非常昂贵,所以我想我只是将User实体的id值设置为用户的电话号码,以便我可以直接通过ID获取。



<问题是电话号码字段是可选的,所以最初创建的用户实体没有电话号码,因此没有id的值。所以它会被创建 user = User(),而不是 user = User(id = phone_number)



因此,当稍后的用户决定将电话号码添加到他的帐户时,是否有将该用户实体的ID值修改为新的电话号码?

解决方案

实体ID构成实体主键的一部分,因此无法对其进行更改。改变它与使用新密钥创建一个新实体和删除旧实体是一样的 - 如果你愿意的话,这是你可以做的一件事。



更好的解决方案是创建一个 PhoneNumber 类型,它提供对相关联的 User 的引用,允许您使用get操作进行查找,但不要求每个用户都只有一个电话号码。


I'm using Google App Engine NDB. Sometimes I will want to get all users with a phone number in a specified list. Using queries is extremely expensive for this, so I thought I'll just make the id value of the User entity the phone number of the user so I can fetch directly by ids.

The problem is that the phone number field is optional, so initially a User entity is created without a phone number, and thus no value for id. So it would be created user = User() as opposed to user = User(id = phone_number).

So when a user at a later point decides to add a phone number to his account, is there anyway to modify that User entity's id value to the new phone number?

解决方案

The entity ID forms part of the primary key for the entity, so there's no way to change it. Changing it is identical to creating a new entity with the new key and deleting the old one - which is one thing you can do, if you want.

A better solution would be to create a PhoneNumber kind that provides a reference to the associated User, allowing you to do lookups with get operations, but not requiring every user to have exactly one phone number.

这篇关于修改Google App Engine实体ID?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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