Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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 1000, most clients don't seem to like starting at 1.

Normally I use mysql and I simply add an sql specific after my table creation:

def self.up
    create_table :reports do |t|
       t.references :something
       ...
    end
    execute("ALTER TABLE reports AUTO_INCREMENT = 1000;")
end

Does anybody know how I can acheive the same for PostgreSQL, ideally I would like the migration to build the table itself so that's not DB specific.

I guess a silly way of achieving my goal would be to create and delete 999 records in a loop, ouch.

like image 660
tsdbrown Avatar asked Sep 15 '09 13:09

tsdbrown


People also ask

Does Postgres automatically create index for primary key?

PostgreSQL automatically creates a unique index when a unique constraint or primary key is defined for a table. The index covers the columns that make up the primary key or unique constraint (a multicolumn index, if appropriate), and is the mechanism that enforces the constraint.

How do I make a column a primary key in PostgreSQL?

To make an existing column the primary key, we can use the "alter table" command, passing the table we're dealing with, which for us is "users". We then specify our action, which is "add primary key", and we pass the name of the column we are making our new primary key.

Is primary key auto increment by default in PostgreSQL?

By simply setting our id column as SERIAL with PRIMARY KEY attached, Postgres will handle all the complicated behind-the-scenes work and automatically increment our id column with a unique, primary key value for every INSERT .

Does PostgreSQL create ID auto increment?

PostgreSQL has the data types smallserial, serial and bigserial; these are not true types, but merely a notational convenience for creating unique identifier columns. These are similar to AUTO_INCREMENT property supported by some other databases.


2 Answers

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 ;-)

like image 164
Michael Krelin - hacker Avatar answered Oct 06 '22 01:10

Michael Krelin - hacker


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
like image 28
Maxim Sloyko Avatar answered Oct 06 '22 01:10

Maxim Sloyko