How can I set the starting point for the primary key (ID) column in Postgres via a rails migration

前端 未结 2 1607
离开以前
离开以前 2020-12-30 12:05

I am deploying a rails app to heroku which uses PostgreSQL as its back-end. In my database migration I normally set the ID field for things likes reports etc to at least 100

相关标签:
2条回答
  • 2020-12-30 12:36

    In postgres, like in many other databases, auto increment feature is done via Sequences. For every Serial and the likes fields sequences are created automatically by Postres for you and named something like TABLENAME _ COLUMNNAME _ seq.

    So, you have to just alter the corresponding sequence, like this:

    ALTER SEQUENCE example_id_seq RESTART 1000 -- corrected from START
    
    0 讨论(0)
  • 2020-12-30 12:39

    Have no idea about rubies and railroads part, but query you're talking about is

    ALTER SEQUENCE reports_something_seq RESTART 1000;
    

    You will have to look up your table for the sequence name and postgresql documentation for general education regarding the matter ;-)

    0 讨论(0)
提交回复
热议问题