Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Alter sequence in a postgreSQL DB using Sequelize

I have a postgresql database that I need to first seed and use predetermine id, then when I create new items to the DB I want the id to auto increment. This was trivial when I was using a SQLite database. When I tried the same code with an PostgreSQL DB I find that the sequelize created IDs have started from zero and have ignored the seedcreated indices, resulting in a unique constraint error.

I figure that this because postgresql uses a separate sequence to keep track of the increment. My approch from here was to use a raw query from sequelize to alter the sequence. But when I try to run an alter sequence query sequelize can't find sequence. I run:

db.query("ALTER SEQUENCE COMPOUND_CPCD_seq RESTART WITH 100;");

But a error is generated and in the error message it states "Unhandled rejection SequelizeDatabaseError: relation "compound_cpcd_seq" does not exist". Is this a problem with upper and lower case letters since COMPOUND_CPCD_seq is not equal to compound_cpcd.seq? And if that is the case how do you work around that (since I have no control over how COMPOUND_CPCD_seq is created)?

What have I missed?

The model:

const COMPOUND= db.define('COMPOUND', {
  CPCD: {
  type: Sequelize.INTEGER,
  primaryKey: true,
  autoIncrement: true,
  unique: true,
 },
  Name: {
  type: Sequelize.STRING,
 }
})
like image 343
Grelnog Avatar asked Nov 04 '25 20:11

Grelnog


1 Answers

First of all you have to check whether the sequence is created in the correct schema of the database where your table is.

If it is correctly created, and the issue is really due to Upper and Lowercase letters, you can put the sequence name in quotes '' to remedy that.

like image 148
Lohit Gupta Avatar answered Nov 07 '25 11:11

Lohit Gupta



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!