App Engine数据存储中的自动增量ID [英] Autoincrement ID in App Engine datastore

查看:119
本文介绍了App Engine数据存储中的自动增量ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用App Engine数据存储,并希望确保行ID与mySQL DB中的自动增量字段类似。



尝试了几个生成策略,但似乎无法控制发生的情况:


  • 这些ID不是连续的,似乎有几个流并行增长。
  • 在旧行被删除后,ids会回收。


    这样的事情是否可能?
    我真的想避免为每一行保留(索引)时间戳。

    解决方案

    听起来像你<一个href =https://stackoverflow.com/a/8544624/101869>不能依靠ID顺序化而没有相当数量的额外工作。但是,有一种简单的方法可以实现您正在尝试执行的操作:


    我们想要删除旧的项目(超过两个月价值为
    例子)

    这是一个自动跟踪其创建和修改时间的模型。只需使用 auto_now_add auto_now 参数就可以实现这一点。

      from google.appengine.ext import db 
    $ b $ class Document(db.Model):
    user = db.UserProperty(required = True)
    title = db.StringProperty(default =Untitled)
    content = db.TextProperty(default = DEFAULT_DOC)​​
    created = db.DateTimeProperty(auto_now_add = True)
    modified = db .DateTimeProperty(auto_now = True)

    然后您可以使用 cron作业任务队列来安排删除旧东西的维护任务。查找最古老的东西就像根据创建日期或修改日期进行排序一样简单:

      db.Query(Document).order(修改)
    #或
    db.Query(Document).order(created)


    I'm using App Engine datastore, and would like to make sure that row IDs behave similarly to "auto-increment" fields in mySQL DB.

    Tried several generation strategies, but can't seem to take control over what happens:

    • the IDs are not consecutive, there seem to be several "streams" growing in parallel.
    • the ids get "recycled" after old rows are deleted

    Is such a thing at all possible ? I really would like to refrain from keeping (indexed) timestamps for each row.

    解决方案

    It sounds like you can't rely on IDs being sequential without a fair amount of extra work. However, there is an easy way to achieve what you are trying to do:

    We'd like to delete old items (older than two month worth, for example)

    Here is a model that automatically keeps track of both its creation and its modification times. Simply using the auto_now_add and auto_now parameters makes this trivial.

    from google.appengine.ext import db
    
    class Document(db.Model):
      user = db.UserProperty(required=True)
      title = db.StringProperty(default="Untitled")
      content = db.TextProperty(default=DEFAULT_DOC)
      created = db.DateTimeProperty(auto_now_add=True)
      modified = db.DateTimeProperty(auto_now=True)
    

    Then you can use cron jobs or the task queue to schedule your maintenance task of deleting old stuff. Find the oldest stuff is as easy as sorting by created date or modified date:

    db.Query(Document).order("modified")
    # or
    db.Query(Document).order("created")
    

    这篇关于App Engine数据存储中的自动增量ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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