如何正确分配_ids?
我希望我的 id 是 < 999999999 但现在尝试编程它似乎具有相反的效果,我只是让 ids 变大,当我尝试分配 ids start > 时结尾 ?
start, end = User.allocate_ids(max=999999999)
logging.info('start %d' % start)
logging.info('end %d' % end)
lower = start if start < end else end
key = User(id=lower).put()
logging.info('key: '+str(key))
user = key.get()
user.add_auth_id(email)
我的日志输出显示分配的 ID 是错误的:
2012-02-13 03:19:07.396 start 98765439124
I 2012-02-13 03:19:07.396 end 98765439123
我该如何解决这个问题?
更新
我最终使用的肮脏的解决方法是制作一个自己的 ID 系统,我不应该这样做,但这是这种情况下唯一的解决方案,我不认为这会产生冲突或重复,如果实体启动,它可能会很慢填充,但就目前而言,这似乎是一个对用户来说可以接受的解决方案,尽管从代码来看可能看起来不太好:
new_id = random.randint(1,999999999)
logging.info('testing new id: %d' % new_id)
while User.get_by_id(new_id) != None:
new_id = random.randint(1,999999999)
logging.info('creating new id: %d' % new_id)
key = User(id=new_id).put()
I want my ids to be < 999999999 but now trying to program that it seems to have the opposite effect, I'm only getting ids larger and when I try to allocate ids start > end ?
start, end = User.allocate_ids(max=999999999)
logging.info('start %d' % start)
logging.info('end %d' % end)
lower = start if start < end else end
key = User(id=lower).put()
logging.info('key: '+str(key))
user = key.get()
user.add_auth_id(email)
My log output show that the ID that gets allocated is wrong:
2012-02-13 03:19:07.396 start 98765439124
I 2012-02-13 03:19:07.396 end 98765439123
How can I fix this?
Update
The dirty workaround I end up using is making sort of an own ID system which I should not do but it was the only solution in this case and I do not think this will create conflicts or duplicates, it just might be slow if entities start filling up, but for now this seems like a solution that works acceptably towards the user though it might not look that good looking at the code:
new_id = random.randint(1,999999999)
logging.info('testing new id: %d' % new_id)
while User.get_by_id(new_id) != None:
new_id = random.randint(1,999999999)
logging.info('creating new id: %d' % new_id)
key = User(id=new_id).put()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
正如 NDB 文档中所述:
allocateIds(max=)
将返回第一个可用的 id,以防您尝试保留已分配的 ID。在您的情况下,之前已经分配了
999999999
之前的所有 id(可能是通过对allocate_ids
的其他调用),98765439124 是第一个可用的 id,98765439123 是最后一个已分配的。请参阅以下示例:
分配 26740080011040 以内的所有 id
26740080011040 以内的所有 id 均已分配,第一个可用的 id 为 26740080011041,最后分配的为 26740080011040
分配全部id 最多26740080011050
As explained in NDB documentation:
allocateIds(max=)
will returns the first id that is available, in case you try to reserve IDs that have already been allocated.In your case all ids up to
999999999
already have been allocated before (maybe by other calls toallocate_ids
), 98765439124 is the first id that is available, 98765439123 is the last one that have been allocated.See the following example:
Allocate all ids up to 26740080011040
All ids up to 26740080011040 already have been allocated, first available ids is 26740080011041, last allocated is 26740080011040
Allocate all ids up to 26740080011050