Sequelize setter error 'invalid input syntax for integer: "[object Object]"'

Issue

I am trying to associate a list of contacts to a customer using the associations setter method, however, it always throws the error ‘invalid input syntax for integer: "[object Object]"’.

The relevant query mentioned in the error is: UPDATE "contactperson" SET "refCustomerId"=$1,"updatedAt"=$2 WHERE "id" IN ('[object Object]')

This is how I use the setter:

db.customer.findByPk(customerID, {
            include: [{
                model: db.address,
                as: 'address',
            },{
                model: db.contactoption,
                as: 'contactOptions',
            }, {
                model: db.contactperson,
                as: 'contactPersons',
            }]
        }).then(customer => {
customer.setContactPersons([ { firstName: 'tester', lastName: 'tester', description: 'lorem ipsum' } ]);
});

This is the association:

Customer.hasMany(models.contactperson, {
  foreignKey: 'refCustomerId',
  as: 'contactPersons'
});

Any idea what I’m doing wrong?

Solution

I managed to resolve this issue using the following code:

db.contactperson.bulkCreate([ { firstName: 'tester', lastName: 'tester', description: 'lorem ipsum' } ]).then(newContactPersons => {
    customer.setContactPersons(newContactPersons);
});

It’s a more complicated approach than intended, but it get’s the job done.

Answered By – Max

Answer Checked By – Dawn Plyler (AngularFixing Volunteer)

Leave a Reply

Your email address will not be published.