我在 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/