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.
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.
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.
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 .
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.
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 ;-)
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With