How to force primary key in ActiveRecord with PostgreSQL to jump start from a specific integer value?

You can execute raw sql like this: ActiveRecord::Base.connection. Execute('ALTER TABLE tbl AUTO_INCREMENT = 1000') Or from any descendant of ActiveRecord::Base (i.e. Any model class), like this: MyRecord.connection.

Execute('ALTER TABLE tbl AUTO_INCREMENT = 1000').

You can execute raw sql like this: ActiveRecord::Base.connection. Execute('ALTER TABLE tbl AUTO_INCREMENT = 1000'); Or from any descendant of ActiveRecord::Base (i.e. Any model class), like this: MyRecord.connection.

Execute('ALTER TABLE tbl AUTO_INCREMENT = 1000').

I'm pretty sure that in PostgreSQL this will fail as there is no auto_increment = in the alter table. What you need is to find the sequence being used and then issue a command like "select setval('seqname',1000);" – Scott Marlowe Sep 26 at 21:36 @ScottMarlowe, you may well be right, I just took the OP's word on the required literal SQL since I am not familiar with PostgreSQL. If you are familiar with it, maybe you can post a better answer than mine?

– Ben Lee Sep 26 at 21:50 Thanks @ScottMarlowe! I have also find the PostgreSQL command before I read your comment. I am using the default ActiveRecord auto generated Primary Key for the class, so it is just named "id", but I don't know the name of the sequence that is associated with its value generation.

– S.Y. Chan Sep 27 at 4:33 Ben Lee, your answer was essentially the right one, how to run raw sql. I just added the last bit. As for the sequence name, can you connect in any way to the db to run a \d tablename and see?

If not, it usually goes by the standard naming convention of tablename_colname_seq. So something like select setval('table_col_seq',1000); – Scott Marlowe Sep 27 at 11:26.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions