knex
semver
>=3.0.0 <4.0.0postconditions24functions15last verified2026-04-11coverage score100%Postconditions — what we check
- select · select-query-errorerrorWhenquery fails (syntax error, connection lost, timeout, invalid table/column)Throws
Error with query details and database-specific error codeRequired handlingCaller MUST wrap select() in try-catch to handle SQL errors, connection failures, and timeouts. Query errors crash application if unhandled.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - insert · insert-constraint-violationerrorWheninsert violates constraint (unique, foreign key, not null, type mismatch)Throws
Error with constraint violation details and database error codeRequired handlingCaller MUST wrap insert() in try-catch to handle constraint violations. Unique violations, foreign key errors, and type mismatches crash application if unhandled.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[2] - update · update-no-try-catcherrorWhenupdate() called in async context without try-catch or .catch() handlerThrows
Error with database-specific error code and query detailsRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[3] - update · update-zero-rows-not-checkedwarningWhenupdate() result (number of affected rows) is not checked after execution — caller assumes update succeeded when WHERE matched nothingThrows
does not throw — returns 0 silentlyRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[3] - delete · delete-no-try-catcherrorWhendel()/delete() called in async context without try-catch or .catch() handlerThrows
Error with database-specific error code; KnexTimeoutError on connection timeoutRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[4] - delete · delete-foreign-key-violationerrorWhendelete() called on a parent row that has child rows in another table with a foreign key constraint, without CASCADEThrows
Error with foreign key constraint violation from database driverRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[4] - first · first-no-try-catcherrorWhenfirst() called in async context without try-catchThrows
Error with database-specific error code on query failureRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[5] - first · first-undefined-not-checkederrorWhenfirst() result used directly without null/undefined check before accessing properties (e.g. result.id, result.name)Throws
TypeError: Cannot read properties of undefined at runtimeRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[5] - raw · raw-no-try-catcherrorWhenknex.raw() called in async context without try-catchThrows
Error with SQL error details from database; or binding Error synchronously on undefined bindingRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[6] - raw · raw-sql-injection-riskerrorWhenknex.raw() called with user-provided input interpolated directly into the SQL string (template literal or string concatenation) instead of using binding parametersThrows
does not throw — silently executes malicious SQLRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[7] - destroy · destroy-not-called-on-shutdownwarningWhenknex instance is created but destroy() is never called — process exits (or tries to exit) without closing the connection poolThrows
does not throw — process hangs indefinitely, never exitsRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[8] - destroy · destroy-no-try-catchwarningWhenawait knex.destroy() called without try-catchThrows
Error on pool teardown failure (rare but possible on network error)Required handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[8] - batchInsert · batch-insert-no-try-catcherrorWhenbatchInsert() called without try-catch around the awaitThrows
Error on constraint violation or connection failure (same as insert()); TypeError for invalid chunkSize or non-array batch inputRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[9] - migrate.latest · migrate-latest-no-try-catcherrorWhenawait knex.migrate.latest() called without try-catch in server startup or deployment scriptsThrows
LockError if migration table is already locked by another process; Error from migration scripts that throw; KnexTimeoutError on database connection failureRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[10] - migrate.latest · migrate-latest-lock-not-released-on-crasherrorWhenProcess crashes during migrate.latest() execution (SIGKILL, OOM, power failure) leaving migration lock in locked stateThrows
LockError('Migration table is already locked') on all subsequent migrate.latest() callsRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[10] - migrate.rollback · migrate-rollback-no-try-catcherrorWhenawait knex.migrate.rollback() called without try-catchThrows
LockError if migration table is already locked; Error from down() migration scripts that throw; Error if named migration not foundRequired handlingCaller MUST handle this condition appropriately.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[10] - transaction · transaction-errorerrorWhenquery within transaction fails (constraint violation, deadlock, connection lost)Throws
Error causing automatic rollback if unhandledRequired handlingCaller MUST wrap transaction callback in try-catch to handle errors and ensure proper rollback. Unhandled errors may leave database in inconsistent state if transaction partially commits.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[11] - increment · increment-no-try-catcherrorWhenincrement() called in async context without try-catch or .catch() handlerThrows
Error with database-specific error code on connection failure, invalid column name, or type mismatch (e.g. incrementing a non-numeric column); KnexTimeoutError after acquireConnectionTimeout on pool exhaustionRequired handlingCaller MUST wrap await knex(table).where({...}).increment() in try-catch.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - increment · increment-zero-rows-not-checkedwarningWhenincrement() result (number of affected rows) is not checked after execution — caller assumes counter was incremented when WHERE matched nothing (row does not exist)Throws
does not throw — returns 0 silentlyRequired handlingCaller SHOULD check the return value of increment(). If the returned count is 0, either the WHERE condition matched nothing or the row was deleted. For counter semantics that require the row to exist, verify count > 0 or use an upsert pattern.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - decrement · decrement-no-try-catcherrorWhendecrement() called in async context without try-catch or .catch() handlerThrows
Error with database-specific error code on connection failure, invalid column name, or type mismatch; KnexTimeoutError on pool exhaustionRequired handlingCaller MUST wrap await knex(table).where({...}).decrement() in try-catch.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - decrement · decrement-zero-rows-not-checkedwarningWhendecrement() result (number of affected rows) is not checked after execution — caller assumes counter was decremented when WHERE matched nothingThrows
does not throw — returns 0 silentlyRequired handlingCaller SHOULD check the returned row count. If 0, the target row was not found. For inventory/seat management, treat 0-row decrement as a precondition failure.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisibleSources[1] - transactionProvider · transaction-provider-factory-no-try-catcherrorWhenawait trxProvider() called without try-catch around the factory invocationThrows
KnexTimeoutError if connection pool is exhausted when the factory is first called; Error on any database-level failure during transaction initializationRequired handlingCaller MUST wrap await trxProvider() in try-catch, especially in high-concurrency contexts where connection pool exhaustion is possible.costmediumin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - transactionProvider · transaction-provider-connection-not-releasederrorWhenFactory invocation succeeds but neither trx.commit() nor trx.rollback() is ever called — transaction is left open indefinitelyThrows
does not throw — connection silently hangsRequired handlingMUST ensure trx.commit() or trx.rollback() is called in all code paths. Use try/finally: const trx = await trxProvider(); try { ...; await trx.commit(); } catch (err) { await trx.rollback(err); throw err; }costhighin prodimmediate exceptionusers seeservice unavailablevisibilityvisible - schema.createTable · schema-create-table-no-try-catcherrorWhenawait knex.schema.createTable(...) called without try-catchThrows
Error if table already exists (database-native: "relation already exists" in PostgreSQL, "Table already exists" in MySQL/SQLite); Error if the connected user lacks CREATE TABLE privileges; KnexTimeoutError on connection pool exhaustion during DDLRequired handlingCaller MUST wrap await knex.schema.createTable() in try-catch in migration scripts. In deployment automation, check for "already exists" errors to handle idempotent re-deployments. Use knex migration system (migrate.latest) rather than manually calling createTable() for idempotent DDL management.costhighin prodimmediate exceptionusers seeservice unavailablevisibilityvisible
Sources
Every postcondition cites at least one of these. Numbered to match the footnotes above.
- [1]knexjs.org/guide/query-builder.htmlhttps://knexjs.org/guide/query-builder.html
- [2]knexjs.org/guide/query-builder.htmlhttps://knexjs.org/guide/query-builder.html#insert
- [3]knexjs.org/guide/query-builder.htmlhttps://knexjs.org/guide/query-builder.html#update
- [4]knexjs.org/guide/query-builder.htmlhttps://knexjs.org/guide/query-builder.html#del-delete
- [5]knexjs.org/guide/query-builder.htmlhttps://knexjs.org/guide/query-builder.html#first
- [6]knexjs.org/guide/raw.htmlhttps://knexjs.org/guide/raw.html
- [7]knexjs.org/guide/raw.htmlhttps://knexjs.org/guide/raw.html#sql-injection
- [8]knexjs.org/guidehttps://knexjs.org/guide/#destroy
- [9]knexjs.org/guide/utility.htmlhttps://knexjs.org/guide/utility.html#batch-insert
- [10]knexjs.org/guide/migrations.htmlhttps://knexjs.org/guide/migrations.html
- [11]knexjs.org/guide/transactions.htmlhttps://knexjs.org/guide/transactions.html
- [12]github.com/knex/knexhttps://github.com/knex/knex/blob/master/lib/query/querybuilder.js#L1111-L1121
- [13]github.com/knex/knexhttps://github.com/knex/knex/blob/master/lib/query/querybuilder.js#L1640
- [14]github.com/knex/knexhttps://github.com/knex/knex/blob/master/lib/query/querybuilder.js#L1124-L1134
- [15]github.com/knex/knexhttps://github.com/knex/knex/blob/master/lib/knex-builder/make-knex.js#L156-L164
- [16]knexjs.org/guide/schema-builder.htmlhttps://knexjs.org/guide/schema-builder.html
- [17]github.com/knex/knexhttps://github.com/knex/knex/blob/master/lib/schema/builder.js#L46-L88
Need a different package?
Request a profile