I have a PostrgresDB i am connected to with my Nodejs app using sequelize, i am trying to create and save a model, but it is throwing error: duplicate key value violates unique constraint "message_pkey"
The problem is that there are entities already in the database, and it is trying to start saving incrementally from ID 1 which is already in the database, how can i make it save starting from the last ID saved in the database ? this is my Model
var Sequelize = require('sequelize');
var db = require('../database/postgres');
var Message =db.define('message', {
id: {
type: Sequelize.INTEGER,
field: 'id',
autoIncrement: true,
allowNull: true,
primaryKey: true
},
time: {
type: Sequelize.DATE,
field: 'time'
},
isread: {
type: Sequelize.BOOLEAN,
field: 'isread'
},
message: {
type: Sequelize.STRING,
field: 'message'
},
messagestatus: {
type: Sequelize.ENUM,
values: ['UNDELIVERED', 'DELIVERED', 'UNREAD', 'READ'],
field: 'messagestatus'
},
receiver: {
type: Sequelize.INTEGER,
field: 'receiver'
},
sender: {
type: Sequelize.INTEGER,
field: 'sender'
},
}, {
tableName: 'message',
timestamps: false
});
module.exports = Message;
First run this in your PostrgresDB :
SELECT setval('TABLENAME_id_seq', (SELECT MAX(id) FROM "TABLENAME"));
// Change TABLENAME with your table
And then try to insert the data.
That's very common issue of sequence from PostrgresDB , For more detail READ
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