更改起始 ID 号 [英] Change starting id number

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

问题描述

我在 Rails 中有一个帐户"模型,在数据库中有相应的帐户"表.如果我擦除数据库并重新开始,account_id"字段将始终从 1 开始并从那里开始计数.我想更改起始编号,以便在新数据库中创建第一个帐户时,account_id"是 1000.有没有办法在 Rails 中做到这一点,或者我需要专门的数据库-依赖SQL代码?

I have an 'Account' model in Rails with its corresponding 'accounts' table in the database. If I wipe the database and start over, the 'account_id' field will always start at 1 and count up from there. I would like to change the starting number, so that, when the very first account is created in a fresh database, the 'account_id' is, say, 1000. Is there a way to do that in Rails, or do I need specialized database-dependent SQL code?

为了说明起见,这里是我的帐户"表的简化版本:

For the sake of illustration, here is a simplified version of my 'accounts' table:

create_table "accounts", :force => true do |t|
  t.string   "email", :null => false
  t.string   "crypted_password", :null => false
  t.string   "name", :null => false
  t.boolean  "email_verified", :default => false
end

推荐答案

您需要执行一些专门的数据库相关 SQL 才能获得此功能.

You'll need to do some specialized database-dependent SQL to get this functionality.

如果您使用的是 MySQL,您可以在 create_table 代码之后添加以下代码到您的迁移中:

If you're using MySQL, you can add the following code to your migration after the create_table code:

execute("ALTER TABLE tbl AUTO_INCREMENT = 1000")

这篇关于更改起始 ID 号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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