0

I tried upsert data using sequelize mode but it will be created duplicated.

what i want if row is not exists insert and if exists update the row.

here is my sequelize model :

module.exports = (sequelize, DataTypes) => {
    return sequelize.define('Contact', {
        id: {
            type: DataTypes.UUID,
            field: 'id',
            allowNull: false,
            autoIncrement: true,
            primaryKey: true
        },
        first_name: {
            type: DataTypes.STRING(100),
            field: 'first_name',
            allowNull: true
        },
        external_id: {
            type: DataTypes.STRING(255),
            field: 'external_id',
            allowNull: false,
            primaryKey: true
        }
    }, {
        schema: 'ec3owner',
        tableName: 'contact',
        timestamps: false
    });
};

and Sequelize method for upsert :

function upsertRow(bodyParams) {
    console.log('bodyParams', bodyParams);
    Contact.upsert(bodyParams, {
            where: {
                external_id: bodyParams.external_id
            },
            individualHooks: true
        })
        .then(function(row) {
            console.log('row', row);
        })
}

Version used :

postgres : 9.6

package.json

"sequelize": "^3.30.4"
"pg": "^6.1.0",
Chandru
  • 10,864
  • 6
  • 38
  • 53

1 Answers1

0

Refer sequelize docs : http://docs.sequelizejs.com/class/lib/model.js~Model.html#static-method-upsert

you must not be providing the id value that's the reason its creating a new record instead of updating .

supravi
  • 59
  • 5