Typeorm doesnt wanna update the database

Viewed 1271

I have a supply table with name and quantity columns. I would expect the following code to update the quantity but it does not. Can someone correct my code please?

const repository = connection.getRepository('supply');
await repository.update({ quantity: 20 }, { name: 'supply1' });
2 Answers

I would try the writing the query like this:

connection.createQueryBuilder()
.update(Supply)
.set({ quantity: 20 })
.where('name = :name', { name: 'supply1' })
.execute();

In this query, Supply should be the entity, not a string. This is based on the documentation for Update Using Query Builder.

Update:

To use the repository.update syntax as you have used in the edited query, you should reorder the arguments.

const repository = connection.getRepository(SupplyEntity);
await repository.update({ name: 'supply1' }, { quantity: 20 });
// executes UPDATE supply SET quantity = 20 WHERE name = "supply1"

Source: Documentation for the repository.api

Also, you might try to console.log(repository) to make sure it exists. Otherwise, you might try passing in the entity as I've done in the code sample above.

Reverse the oreder:

await repository.update({ quantity: 20 }, { name: 'supply1' }); // tries to update where quantity is 20...
await repository.update( { name: 'supply1' }, { quantity: 20 }); // updates quantity

Related