2

I have a User migration file in rails project. I want to make my user table id start from 10000. Like this link:

execute "ALTER TABLE users AUTO_INCREMENT = 10000;"

After this, I want to set id smaller than 10000. For example, I want to make william's id is 1, how should I do it?

2
  • i doubt it can be done.. did you try creating a record and assigned id manually
    – SRDP
    Commented Mar 26, 2015 at 8:08
  • @SRDP It seems work. Actually i tried it. But i made a typo mistake. Thanks anyway!
    – William Hu
    Commented Mar 26, 2015 at 8:22

2 Answers 2

1

From this article 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.

But actually i can create User by assign ":id" property equal smaller then 10000.

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

Thanks @SRDP too.

IMPROTANT: You user id may start from 1 if you set :id = 1 first. So set a user from 10000 first, then create any user below 10000.

0

Try the below,but i wont suggest its a good practice .

Create the record first so it will have some id ie 10067 or something now suppose you want assign thsi record to a id which is not created in your case ie id < 1000 you can do is

User.where(id: 1).update_all id: 10067 

Not the answer you're looking for? Browse other questions tagged or ask your own question.