mysql - 如何在 rails 中分配 AUTO_INCREMENT 值

标签 mysql ruby-on-rails

我在 Rails 项目中有一个 User 迁移文件。我想让我的用户表 ID 从 10000 开始。像这样 link :

execute "ALTER TABLE users AUTO_INCREMENT = 10000;"

在这之后,我想设置id小于10000,比如我想让william的id为1,我应该怎么做呢?

最佳答案

来自这篇文章link

ALTER TABLE table_name AUTO_INCREMENT = value;

You specify the table name after the ALTER TABLE clause and the value which we want to reset to in the expression AUTO_INCREMENT = value.

Notice that the value must be greater than or equal to the current maximum value of the auto-increment column.

但实际上我可以通过分配小于 10000 的“:id”属性来创建用户。

User.create(:id=>1, :name=>'William') 

也感谢@SRDP。

重要提示: 如果您先设置 :id = 1 ,您的用户 ID 可能从 1 开始。所以先从10000开始设置一个用户,然后再创建10000以下的任何用户。

关于mysql - 如何在 rails 中分配 AUTO_INCREMENT 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29273518/

相关文章:

ruby-on-rails - Nginx - 如何配置 IP 地址? (仅返回 127.0.0.1)

ruby-on-rails - 如何在 Rails 3 中将 Thin 设置为默认值

python - 在 Python 中执行 MySQL 查询无法正常工作

mysql - JBoss 新数据源向导中无法选择 MySQL 驱动程序

php - 我如何在 Agiletookit 中调试与数据库相关的错误?攻击力4

mysql - 与带有重音符号、变音符号等的单词匹配 mysql/php

ruby-on-rails - Heroku 中的静态只读 SQLite3 数据库

ruby-on-rails - 默认图像的 CarrierWave 完整 url

ruby-on-rails - 在 Rails 表单对象中处理日期的最佳方式是什么

php - 地名城市 [邮政编码] 表