Devise用户ID以自动递增的方式跳过几个数字 [英] Devise User ID skips several numbers in auto increment

查看:62
本文介绍了Devise用户ID以自动递增的方式跳过几个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一段代码以编程方式创建了一个新的devise用户:

I have section of code that programmatically creates a new devise user:

    user = User.new
    user.username = params[:username]
    user.email = params[:email]
    user.password = params[:password]

    if user.save
        render :json => user
    else
        render :json => user.errors
    end

我最近注意到 user id 已跳过我的记录中的大约30个数字。

I have recently noticed that the user id has skipped about 30 numbers in my records.

看看我的记录:

任何想法可能导致增量中的巨大缺口吗?用户无法删除帐户。

Any idea what might have caused this huge gap in the increment? Users have no way to delete their account.

推荐答案

如果您部署在Heroku上,则使用的是Postgres。在Postgres中,主键ID只能按线性,自动递增的顺序分配一次。但是,如果事务回滚,则分配的ID将 still 标记为已使用,并且将 not 再次分配。

If you're deployed on Heroku, you're using Postgres. In Postgres, primary keys ids are allocated just once in an linear, autoincremented sequence. But in the event that a transaction is rolled back, the allocated id will still be marked as used and will not be allocated again.

不可能确切地说,但我的猜测是在 30 61 由于某些事务错误而被回滚。您可能想对存储库提交进行交叉检查,以查看是否对 User 模型进行了任何修改,这些修改可能与没有用户提交数据库的时间段相对应。

It's impossible to tell definitively, but my guess is that the creation of all users between 30 and 61 were rolled back due to some transactional error. You may want to crosscheck against your repository commits to see whether you made any modifications to your User model that might correspond to the time frame during which no users were committed to the DB.

基本上,事情在一开始就起作用了,然后又坏了,现在又可以了。检查 User.find(29).created_at User.find(62).created_at 之间的时间段任何提交都可能影响您的模型。

Basically, things were working at one point, then they broke, and now they're working again. Check the period between User.find(29).created_at and User.find(62).created_at and see whether any commits may have impacted your model.

这篇关于Devise用户ID以自动递增的方式跳过几个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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