How to update and delete with Knex — Basic
Change or remove rows using update(), del(), or delete() with filters.
Learn: update(patch) and del() (or delete()) should always include where filters. Check the number of affected rows to distinguish 404 from success.
Supplementary examples
Update and delete by id
function update(id, patch) {
return knex("pastes").where({ paste_id: id }).update(patch);
}
function remove(id) {
return knex("pastes").where({ paste_id: id }).del();
}Course example
function read(supplier_id) {
return knex("suppliers").select("*").where({ supplier_id }).first();
}
function update(updatedSupplier) {
return knex("suppliers")
.select("*")
.where({ supplier_id: updatedSupplier.supplier_id })
.update(updatedSupplier, "*");
}
Additional references & examples
- Knex.js documentation
Query builder, migrations, seeds, and connection configuration.
- PostgreSQL documentation
SQL syntax, data types, and database administration reference.