2

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?

2
  • I was thinking the error might be in "WHERE "id" IN ('[object Object]')2, shouldn't it show an array of integers rather than an object? Commented Feb 13, 2021 at 18:38
  • Sorry, yes, obviously! I completely read that wrong - was not awake enough yet, I guess ! Commented Feb 13, 2021 at 21:51

3 Answers 3

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I used create to create just one entry.const user = await User.findOne({ where: { uid:uid } }) const productUser = await Product.create(req.body) await user.addProduct(productUser)
2

You used set<ModelName>s that just updates a link field of given records. If you need to create contactperson record you need to use createContactPerson instead of setContactPersons (NOTE: you cannot create several records at once).

customer.createContactPerson({
  firstName: 'tester',
  lastName: 'tester',
  description: 'lorem ipsum'
});

compare to:

const contactPerson = db.contactperson.findById(1);
if (contactPerson) {
  customer.addContactPersons([contactPerson.id]);
}

set<ModelName>s - replaces old linked records with the new existing ones add<ModelName>s - adds exisiting records in addition to old ones create<ModelName> - create a new non-existing record in addition to old ones

See hasMany special methods

1 Comment

Using addContactPersons yields the same result: [ { firstName: 'Test', lastName: 'Tester', description: 'Lorem ipsum' } ] Executing (default): UPDATE "contactperson" SET "refCustomerId"=$1,"updatedAt"=$2 WHERE "id" IN ('[object Object]') (node:11392) UnhandledPromiseRejectionWarning: SequelizeDatabaseError: invalid input syntax for integer: "[object Object]"
0

Exactly what Anatoly posted. I had method declared on TypeScript like:

declare addPost: HasManyCreateAssociationMixin<PostClass, 'userId'>;

When i changed to:

declare createPost: HasManyCreateAssociationMixin<PostClass, 'userId'>;

Everything works so remember - how you describe name of method its very necesary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.