LoginSignup
0
2

More than 5 years have passed since last update.

auto_incrementの初期値を設定したい

Posted at

やりたかったこと

とある理由であるテーブルのidが1からスタートではなく、
1001からスタートしてほしいという依頼が来た。

コード

まずmigrationファイルを作成する。

$rails g migration id_autoincrement_set

と実行するとmigrationファイルが作成されます。

class IdAutoincrementSet < ActiveRecord::Migration[5.2]
  def change
  end
end

中身はこのような感じだと思います。

class IdAutoincrementSet < ActiveRecord::Migration[5.2]
  def change
     execute "ALTER TABLE samples AUTO_INCREMENT = 1001"
  end
end

として管理画面などから記事を作成すればidが1ではなく、
1001から作成されてることがわかると思います!

0
2
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2