Skip to content

Changelog ​

4.4.4 ​

Fixes ​

  • upsertGraph() doesn't run the $beforeUpdate() hooks of existing models that it only relates, e.g. through a join table or a BelongsToOneRelation, if they have props that aren't written to the database, like props that $formatDatabaseJson() or pickJsonSchemaProperties leave out. Their ids counted as changed, as there is no current model to compare them with, although nothing is written to their rows
  • distinct() without columns makes the selected rows distinct, as in knex, e.g. in Person.query().select('name').distinct(). Objection dropped it without an error. The query still selects all columns of its table by default, e.g. select distinct "persons".* from "persons" #557
  • unrelate() only clears the columns of a key that aren't part of the id of their model, e.g. the tenantId of multi-tenant keys like (tenantId, ownerCode), which it set to null before. upsertGraph() does the same when it removes a BelongsToOneRelation row, which it rejected before. If all columns of the key are part of the id, both throw, as only deleting the rows can remove the relation #561
  • MySQL: many-to-many unrelate(), and patch() with join table extras, work with keys that share a column, e.g. a tenantId in both keys of the join table. Since 3.7.0, they failed with "Operand should contain 4 column(s)" #565
  • insert() and relate() through an owner model without a key, e.g. one that was never saved, whose insert didn't return an id, or whose key column is null, throw an error that names the relation and the missing key, before any query runs. They failed with a TypeError, after inserting the related row in many-to-many relations, or updated nothing without an error in BelongsToOneRelation and BelongsToManyRelation. A null key in the owner made HasManyRelation insert and relate rows with a null foreign key #568
  • Relation writes that are filtered through joinRelated() or its variants keep the joins: many-to-many unrelate(), patch() and delete(), belongs-to-one unrelate(), and on MySQL has-many relate() and unrelate(). They lost them and failed with an unknown column #560
  • MySQL: patch() and delete() with a subquery that joins the updated table under an alias, e.g. join('Person as owner', ...), join({ owner: 'Person' }, ...) or joinRelated(), wrap the subquery, as they already did for joins without an alias. They failed with "You can't specify target table 'Person' for update in FROM clause" #564
  • upsertGraph() works for models whose onBuild() hooks add where clauses to all queries, e.g. to leave out soft-deleted rows. It threw "upsertGraph query should contain no other query builder calls like findById, where, …", as it checked for such calls after the hooks ran. Where clauses that a caller adds through onBuild() on the upsertGraph() query itself are no longer rejected either, and have no effect #563
  • When the hook of one model rejects asynchronously and the hook of a later model throws, e.g. in $beforeInsert(), $afterFind() or a relation's beforeInsert, or a traverser of traverseAsync() does, the query rejects with the second error and handles the first. It was left as an unhandled rejection, which ends Node processes by default #566

4.4.3 ​

Fixes ​

  • Queries that mix withGraphJoined() and withGraphFetched() and select all columns of the root table, e.g. with select('*') or select('Person.*'), load the fetched relations again. Since 3.8.0, withGraphJoined() replaces the * with the table's columns, which made the query skip the build step of withGraphFetched(), so its relations were missing from the result without an error #2269
  • An onBuild() hook that removes operations, e.g. with clearSelect(), no longer makes the query skip building the operation after them, e.g. the where of findById(), which made the query read, update or delete all rows

4.4.2 ​

Fixes ​

  • upsertGraph() doesn't update models whose given props are unchanged apart from props that aren't written to the database, like props that $formatDatabaseJson() or pickJsonSchemaProperties leave out. They counted as changed, and the update ran the $beforeUpdate() hooks, which could still write e.g. timestamps #546

4.4.1 ​

Changes ​

  • Strict insertGraph() and upsertGraph() resolve the settings of each relation path once instead of for every model and option, which speeds up large graphs with many relations entries

4.4.0 ​

Changes ​

  • Add Model.getGraphSettings(), which resolves the strict mode settings of insertGraph() or upsertGraph() for a relation path from the options of a call, with the same rules and defaults as the call itself #537

Fixes ​

  • Strict insertGraph() with added: 'relate' throws a NotFoundError for related models whose ids don't exist, before anything is written, like upsertGraph() does. Since 4.3.0, it related them unchecked, writing keys that point to no row unless a foreign key constraint rejected them #538

4.3.0 ​

Changes ​

  • Add a strict option to insertGraph() and upsertGraph() that makes graphs say what they mean. Options are given per relation, as root and relations settings for the rows that a graph leaves out (removed), the rows with ids that aren't in their relation yet (added), the rows that stay (kept) and the new rows (created). In strict mode, they replace the per-path options like relate, unrelate and noDelete. See strict mode #429
  • Strict graphs throw where other graphs skip or override data: for rows they don't write, for key props that contradict their relations, and for values they don't write that differ from the stored rows. Equal values pass, so fetched graphs can be sent back unchanged #429
  • Strict insertGraph() and upsertGraph() are atomic: they run in a transaction, their own unless the query already runs in one, so a database error halfway leaves nothing written. Hooks that run queries need to use context.transaction, or they wait for the transaction, and deadlock on SQLite #477

Fixes ​

  • The typings of the upsertGraph() options declare fetchStrategy, which TypeScript rejected since 1.6.4 added the option #531

4.2.0 ​

Changes ​

  • patchAndFetch(), patchAndFetchById(), updateAndFetch() and updateAndFetchById() throw when the same query is executed a second time, e.g. awaited twice. Since at least 1.0.0, the second run wrote back the row that the first one fetched, undoing other writes in between #519

Fixes ​

  • patchAndFetchById(), updateAndFetchById(), patchAndFetch() and updateAndFetch() with an empty patch return the unchanged model if the row matches, and undefined only if it doesn't. Since 1.5.0 they always returned undefined #517
  • asFindQuery() in static after hooks runs the onBuild hooks of the query's context once. Since 2.0.0 it ran them a second time on the built write query, e.g. adding a join twice

4.1.0 ​

Changes ​

  • On PostgreSQL, json field expressions with a single key use -> and ->>, e.g. "props"->>'points' for ref('props:points').castText(), so that expression indexes like ((props->>'points')) match them. Deeper paths and keys that can be array indexes, like [0], 0 or -1, keep #> and #>>. This changes the generated SQL: an index built on the #>>'{points}' form no longer matches and needs to be rebuilt with ->> #508

Fixes ​

  • On PostgreSQL with standard_conforming_strings off, json keys with backslashes in field expressions read the right values. Since 3.3.0 they read other keys or threw "malformed array literal"
  • ref().castText() on a JSON path compares with string columns on MySQL with the mysql2 driver, e.g. in the joins of JSON relations. Since 4.0.0 it threw "Illegal mix of collations". Comparisons with string values, orderBy(), groupBy() and distinct() on these values now compare bytes instead of using the connection's collation, so they're case-sensitive like on Postgres
  • In strict mode, editors suggest the columns of the model as keys of the objects that where(), findOne() and having() take. Since 4.0.0 they suggested none

4.0.0 ​

Upgrading from 3.x? The migration guide explains each breaking change and what to do about it.

Breaking changes ​

Packaging ​

  • objection is an ES module package with an exports map and no default export. Deep imports like objection/lib/... no longer work. See migration #140
  • The typings are ES module declarations instead of export =. See migration
  • Node 20.19 or newer is required (migration)
  • Knex 3.3 or newer is required (migration) #95
  • MySQL 8.0.17 or newer is required (migration) #292
  • The typings require TypeScript 5.8 or newer (migration) #485
  • The database error classes are objection's own, so instanceof checks against the classes of the db-errors package no longer match. See migration
  • Errors of knex's better-sqlite3 client are wrapped in objection's error classes, e.g. UniqueViolationError, instead of reaching you as SqliteError
  • Model files given as paths are loaded with require(), so ES module model files need to export the model class as default or as their only class, and can't use top-level await. See migration

upsertGraph() and insertGraph() ​

Both are rewritten. They now write a graph in the order that its rows depend on each other, and reject graphs that contradict themselves before writing anything.

  • They write in far fewer queries, e.g. one delete per relation, so static hooks get several models at once. A custom insert() that forwards arrays to insertGraph() now recurses forever. See migration
  • They throw for onConflict(), ignore() and merge(), which 3.x ignored (migration) #2156
  • They throw for unknown options, and for option values of the wrong type even when no model uses them (migration) #84
  • insertGraph() throws for new models below models that it relates (migration) #217
  • A graph that gives the same row two different values for a column, or two rows to one HasOneRelation, throws InvalidGraph. A many-to-many pair gets one join row. See migration
  • A #ref or #ref{} to a new model that the graph doesn't insert throws InvalidGraph, and so do cycles of #ref{} values. See migration #53 #427
  • Below a model that noInsert skips, only the models that a #ref points to are kept. See migration #369
  • Validation error paths of array input start with the index, e.g. [1].pets[0].name, and the returned models no longer contain #id, #ref or other markers. See migration
  • Custom relation classes that extend Relation directly need a keySide getter that says where their keys are stored: 'owner', 'related' or 'join'. See migration
  • upsertGraph() writes models below related models together with the rest of the graph, instead of in nested calls at the end. See migration #427
  • upsertGraph() moves rows to their new owners before it deletes, so a row moved out of a deleted parent is kept. Swapping rows on a unique key now needs the unrelate option. See migration #214
  • upsertGraph() checks that the models it relates exist, and throws a NotFoundError before writing anything. See migration #372
  • upsertGraph() doesn't patch models that didn't change, so their update hooks don't run, and throws a NotFoundError when a row it updates is gone. See migration #214 #415
  • When a graph changes a row's key, upsertGraph() writes the new key first, and uses it wherever the row appears in the graph. See migration #427 #428 #491
  • upsertGraph() sets the owner's foreign key to null before it deletes a BelongsToOneRelation model, also when the owner's schema doesn't allow null. See migration #212
  • upsertGraph() keeps the current HasOneRelation model when the graph gives a different model marked with #unrelate or #delete, or a replacement that noInsert or noRelate skips. See migration #261
  • upsertGraph() relates a BelongsToOneRelation model that only gives its key, like pet: { code: 'c1' }, without looking it up, and throws InvalidGraph if it also gives other props or relations

Hooks ​

  • The result of static after* hooks has the shape that the query returns: a model, undefined, a number or an array. asArray() turns it into an array. See migration #66 #210
  • The static after* hooks run last, after first() and the fetch of *AndFetch(), and runAfter() callbacks run after them. See migration #66
  • Find hooks only run for queries that actually run, so no longer for the input models of upsertGraph(), and beforeFind no longer for models that fetchGraph() already has. See migration #66
  • withGraphFetched() uses the models that afterFind returns, so the hook can filter or reorder them, and withGraphJoined() calls afterFind too. See migration #1963

Queries ​

  • skipUndefined() is removed, and undefined values passed to where() and the other query methods throw. See migration #53
  • findById(1).orWhere(…) can no longer return other rows: the id condition gets its own parentheses. See migration #271
  • Aggregates like count() select only their alias, and queries that load relations add the join columns they need, which grouped queries must group by. See migration #111
  • Writes with returning() on a to-one $relatedQuery() resolve to one model or undefined, not an array. See migration #53
  • offset() in updates and deletes throws, and so does orderBy() with limit() in a MySQL delete. See migration #286 #235
  • Many-to-many patches with join table extras run in one query, which needs SQL Server 2016 or newer. See migration #287
  • idColumn = undefined means that the model has no primary key, like null #129

Relations ​

  • limit(), offset() and first() in withGraphFetched() modifiers apply to each parent, not to all of them together. Set limitPerParent: false for the 3.x behaviour. See migration #127 #429
  • Relation expressions that name a relation twice (migration) or have anything after * or ^ (migration) throw #131 #221
  • Fetching a relation throws when the models lack the columns it joins on, instead of finding nothing. See migration #1832 #2258 #114
  • $setRelated() and $appendRelated() update the foreign keys to match. See migration #2001
  • unrelate() of a BelongsToOneRelation respects the relation's modify / filter, instead of unrelating all owners #234

JSON ​

  • On PostgreSQL, JSON attributes are no longer parsed a second time, so JSON stored in text columns stays a string. Set jsonColumnFormat = 'text' for those. See migration #205
  • snakeCaseMappers() no longer converts JSON keys in field expressions like 'data:someKey'. See migration #1089
  • insert() and update() reject field expression keys like 'meta:color'. Use patch(). See migration #160
  • JSON field expressions and the whereJson*() methods throw on SQL Server and Oracle, instead of warning. See migration #113
  • On MySQL, the casts of ref() and val() use MySQL's own types, and objects and arrays in val() are JSON values on MySQL and SQLite. See migration #276

Typings ​

  • Custom query builders declare one RebindType instead of five *QueryBuilderType members. See migration #125
  • insert(), patch(), update() and relate() reject relation properties, which they ignore at runtime. See migration #53
  • toJSON() returns plain JSON types instead of model instances. See migration #2380
  • $relatedQuery() of to-one relations may resolve to undefined. See migration #2355
  • Smaller type changes: RelateQueryBuilder (migration), readonly jsonSchema (migration) and virtualAttributes, no $ properties in ModelObject (migration), relate().returning() rows (migration), no internal helper types (migration), no allowMarkers (migration), and distinctOn() takes only columns, raw() and ref() #16 #319 #433 #84

Features and fixes ​

Packaging ​

  • wrapError() is exported, to wrap errors of the database driver that don't come from objection's queries

upsertGraph() and insertGraph() ​

  • #ref sets the foreign key in a HasManyRelation or HasOneRelation #311
  • #ref{} works in foreign keys that the relation also sets #1378, and reads values that the graph fills in, like the key of an inserted model #356
  • A many-to-many #ref writes only its own join table extras, instead of copying those of the model it points to
  • A row can move out of a HasOneRelation while a new row takes its place #368 #398
  • Keys that the database generates, like a uuid that isn't the id, are fetched before rows that join through them are written #853
  • New models get the keys of their parents that the graph leaves out, instead of NULL
  • With relate and noUpdate, HasManyRelation and HasOneRelation models are related, instead of silently left alone #53
  • Foreign keys in a JSON path, like pet.data:sitterId, are written without losing the other values of the column #488
  • A failed graph waits for its running queries before it rejects, so a transaction rolls back after them #335
  • Large graphs stay under the number of values that a database can bind in one query
  • Several models without properties can be inserted on PostgreSQL #1694
  • Rows whose foreign keys point to each other are patched one after the other, which avoids deadlocks on MySQL #385
  • upsertGraphAndFetch() applies the modifiers and aliases of withGraphFetched() #839
  • Models returned by upsertGraph() write all their props when patched later #371
  • Loaded many-to-many models no longer carry internal objectiontmpjoin* aliases, e.g. in the items of delete hooks #370
  • The error for upsertGraph() queries with other query builder calls mentions $query(), which adds such a call too #1168
  • A #ref at the root, or a graph that changes part of a composite id, throws InvalidGraph before anything is written, and BigInt ids throw on SQLite, which can't bind them

Hooks ​

  • The hooks of relate() and unrelate() get the relation and its owners #66
  • cancelQuery() with a single row in beforeInsert is used as the inserted model, instead of being ignored #66

Queries ​

  • insert() of several models works on SQLite 3.35 or newer, and on all databases when every model has its id
  • Many-to-many patches with join table extras and limit() can order by join table columns, and patch the right join rows on MySQL and SQL Server #161 #360
  • asFindQuery() in the hooks of these patches works for more rows than SQL Server can bind #287

Relations ​

  • New BelongsToManyRelation, for owners that hold an array of related ids, e.g. post.tagIds. See relations #2667
  • modifyJoin() changes the ON clause of the joins of joinRelated() and withGraphJoined() #1975

JSON ​

  • JSON field expressions and the whereJson*() methods work on MySQL and SQLite, except whereJsonSupersetOf() and whereJsonSubsetOf() on SQLite. See JSON queries #276
  • patch() with field expression keys writes into NULL columns, and still works when the query is built twice #53
  • insert() on SQLite stores arrays as JSON, instead of '[object Object]'

Typings ​

  • Strict mode, opt-in with TypeConfig { strict: true }, checks the columns, values, relations and modifiers of queries against your models. See models #16 #431 #432
  • FromModel<typeof Model> derives a model's properties from its jsonSchema and relationMappings. See models #16 #430
  • patch() and merge() accept field expression keys like 'address:street'
  • Faster type checking, and fixes in ModelProps, ModelRelations, ModelClass<M> and whereIn()

Other ​

  • The tests run on vitest, and the examples and docs use ES modules

3.9.1 ​

Fixes ​

  • insertGraph() and upsertGraph() reject only after the queries they already started have finished, so a transaction is rolled back after them. Before, rows of these queries could be committed after the rollback on SQLite #336
  • upsertGraph() sets the foreign key of a model that a #ref relates into a HasManyRelation or HasOneRelation. Before, the row was left without it, and the id of the owner could be cleared #311
  • upsertGraph() with relate and noUpdate writes the foreign keys of models that it relates through a HasManyRelation or HasOneRelation, like it does for BelongsToOneRelation and ManyToManyRelation, instead of leaving them unrelated without an error, as it did since 1.5.0 #355
  • upsertGraph() resolves a #ref{} to a value of an existing model, e.g. a foreign key that an inserted related model gives it, once the graph has filled it in. Before, it read the old value, e.g. undefined #356, #378
  • upsertGraph() rejects a graph with cyclic references before it writes anything. Before, it still ran the deletes of the rows that the graph leaves out after the rejection, which deleted them without a transaction and failed after the rollback of one, since 1.5.0 #379
  • A many-to-many patch() / update() with join table extras and limit() on MySQL and SQL Server writes the extras to the join rows of the related rows that the limit picks when the relation joins through a related column other than the id, instead of to the join rows that refer to other rows by these ids, as it did since 3.8.0 #360

3.9.0 ​

Fixes ​

  • Behaviour change: upsertGraph() replacing the row of a BelongsToOneRelation, e.g. { id: 1, pet: { id: 2 } }, points the owner to the new row before it deletes the old one. The static delete hooks of these deletes get no relation or items in their context, as the rows are deleted by id. Before, the old row was deleted first: with a NOT NULL foreign key and ON DELETE CASCADE, the database deleted the owner and upsertGraph() still succeeded, and foreign keys without ON DELETE action failed. A new row that repeats a unique value of the row it replaces now fails, as both exist at the same time: pass the id of the old row to update it instead. When noInsert or noRelate skips the replacement, the current row and the owner's reference are kept.
  • Behaviour change: upsertGraph() removing the row of a BelongsToOneRelation with pet: null or #delete sets the owner's foreign key to null before it deletes the row if the owner's jsonSchema allows null for it (in type, anyOf, oneOf or with nullable: true). Before, the row was deleted first: with ON DELETE CASCADE the database deleted the owner, and foreign keys without ON DELETE action failed. The owner is now patched, also with ON DELETE SET NULL, so its update hooks run (e.g. writing updatedAt), and the static delete hooks of these deletes, like those of replaced rows, get no relation or items in their context. Without a nullable schema for the key, the row is still deleted first and the owner's reference is left to the database. #212
  • Behaviour change: upsertGraph() no longer deletes a row that is missing in one place of the graph if the same graph relates or updates it in another place. The row is unrelated from its old place instead, so rows can be moved between parents in one call, also below related models. #214
  • Behaviour change: upsertGraph() with a #unrelate or #delete marker on a BelongsToOneRelation model that isn't the currently related one keeps the current one, as the marked model is ignored. Before, the current row was deleted without clearing the owner's foreign key. #213
  • Behaviour change: insertGraph() and upsertGraph() no longer insert the related models of models that noInsert skips, unless a #ref points to them. These were inserted without foreign keys. #215
  • insertGraph() logs a warning when it ignores new models below related models. This will throw in objection 4.0. Use upsertGraph() or a separate query to insert them. #217
  • Behaviour change: the graph options reject * and ^ in brackets with other relations, like children.[pets, ^], instead of never matching them #221
  • The #ref and #dbRef markers are removed from the results of insertGraph() and upsertGraph(), also from models they skip #221
  • upsertGraph() no longer throws "model (id=…) is not a child of model (id=…)" for children with ids below nodes related through a relate path option #220
  • $relatedQuery().insertGraph() with several models on many-to-many relations inserts the join rows on SQLite and MySQL, where it failed #219
  • $relatedQuery().insertGraph() on a BelongsToOneRelation relates the inserted row to the owner #216
  • Behaviour change: deleteById(), patchById() and updateById() also keep their id condition in its own where group, which the 3.8.0 notes already claimed for "the other *ById methods": deleteById(1).where(a).orWhere(b) runs id = 1 and (a or b) instead of id = 1 and a or b, which deleted other rows. A findById() added by the user stays in the user's where group, also in findById(1).patch(…). #232
  • Behaviour change: a many-to-many patch() / update() that only changes join table extras returns the number of related rows whose join rows it updated, instead of 0. The static afterUpdate hooks receive this number as their result instead of 0. patchAndFetchById() with only extras returns the patched row instead of undefined. #235
  • Behaviour change: on SQL Server, a many-to-many patch() / update() with join table extras applies limit(), offset() and orderBy() to the related rows it picks, and only updates their join rows, as on MySQL. Before, the limit applied to each batch of related rows, all join rows were updated, and an orderBy() with the limit failed with a syntax error. With limit().offset() but no orderBy() it now throws, as SQL Server rejects OFFSET without ORDER BY, where the offset was ignored before. #233
  • Many-to-many patch() / update() with join table extras select the related ids straight from their filter query, unless limit() or offset() picks the related rows on MySQL or SQL Server. Postgres could plan the previous query as a nested loop that ran for minutes #161
  • On MySQL, delete() with both orderBy() and limit() warns that the order has no effect, as knex leaves it out of the statement. This will throw in objection 4.0. #235
  • Behaviour change: query properties like raw(), ref() and subqueries given to a model instance are no longer written again once their keys are given a value, by assignment, $set(), $setJson() or a write that fetched the value. $setJson() keeps the query properties of earlier calls, $setJson(modelInstance) drops them for the keys the instance has, values that objection sets itself (e.g. foreign keys set by insertGraph() and $relatedQuery().insert()) beat a query property for the same key, values that $beforeInsert() / $beforeUpdate() set for keys given as query properties are removed from the instance after the hooks, as the query properties are written instead, and schema defaults no longer appear on the model or in toJSON() for keys given as query properties. #202
  • raw() and other query properties assigned to json attributes in $beforeInsert() and $beforeUpdate() are written instead of their serialized objects #203
  • Behaviour change: pickJsonSchemaProperties keeps field expression keys like 'meta:a' whose json column is in the schema, so patch(), update() and upsertGraph() write them instead of silently dropping them. insert() no longer silently drops such keys either: they reach the database and fail as they do without pickJsonSchemaProperties. Keys only found on Object.prototype (like 'toString') are no longer picked. upsertGraph() no longer selects field expression keys as columns. #204
  • Behaviour change: a patch(), update() or merge() that sets both a json column and a field expression into it throws a ValidationError, also when a model instance holds both, instead of dropping the field expression or failing with an SQL error #206
  • Behaviour change: patches and the field expressions of update() are validated without the required properties of schemas referenced by $id, like the ones added in onCreateAjv(), which regressed in 3.8.0. Whole values for such properties in patches are validated without required too, like local definitions. #207
  • onConflict().merge() with a json field expression key no longer fails with an ambiguous column on Postgres. merge({ 'meta:b': ref('meta:a') }) is still ambiguous: qualify the reference with the table, e.g. ref('items.meta:a'). #260
  • Behaviour change: hasSelection() and findSelection() treat a table name without schema as the schema-qualified table, so withGraphJoined() no longer throws for selections like persons.id of a model with a tableName like public.persons, a regression in 3.8.0, and replaces its persons.* selections #226
  • Behaviour change: withGraphJoined() tells apart rows whose ids are Buffers, e.g. binary UUIDs, and withGraphJoined() / withGraphFetched() tell apart keys whose dates differ only in milliseconds, composite keys whose strings contain commas, and string ids that contain / or ., also in upsertGraph(). Dates are keyed by their time, so a Date and a number with the same epoch milliseconds match, e.g. Date ids passed to fetchGraph() and the numbers SQLite returns for them. #222 #223
  • resultSize() and the total of page() and range() respect distinctOn() with withGraphJoined() on Postgres, also on joined columns #224
  • limitPerParent limits the related items when the relation key is a JSON field #225
  • withGraphFetched() with the limitPerParent option works for related models with a schema-qualified tableName, also when the order names the schema #265
  • Behaviour change: when SQL Server reports several errors for one statement, e.g. for offset() without orderBy(), objection throws a DBError with all their messages, instead of the driver's AggregateError with an empty message, which is kept as nativeError
  • update(), patch() and delete() warn about offset(), as knex leaves it out there, and on SQL Server the limit too. This will throw an error in objection 4.0. #286

Types ​

  • Custom query builders that override withGraph() compile again, also with TypeScript 7, which broke in 3.7.0 with the relate().returning() typings

Docs ​

  • withGraphFetched() doesn't assign related rows whose keys only match by collation: by case or accents on MySQL, by case or trailing spaces on SQL Server #227
  • upsertGraph() matches ids by their exact value, and how to normalize uuids given in a different case #221

3.8.0 ​

What's new ​

  • The limitPerParent graph option applies limit(), offset() and first() of relations loaded with withGraphFetched() per parent instead of to all parents together, e.g. withGraphFetched('pets(latest)', { limitPerParent: true }). With page() / range() the relation query is limited as before. On MySQL before 8, relations with a limit are fetched with one query per parent. withGraphJoined() ignores the option and warns about it. #127 #429

Fixes ​

  • Behaviour change: update() validates field expression keys like 'meta:color' like patch() does since 3.3.0: the written parts are validated against the json column's schema, including $refs, without its required, while the rest of the object is validated normally. The json column counts as present and doesn't get its default. Before, the keys were validated as top-level properties: invalid values passed, additionalProperties: false rejected the keys, a required json column counted as missing, and its default replaced the whole stored value. #160
  • Behaviour change: keys containing : that the schema declares in properties, e.g. 'ns:name', are normal properties and columns: validated as properties and written to their column by patch(), update() and merge(). Before, patch() (since 3.3.0) and update() / fromJson() treated them as field expressions and wrote them as JSON paths. Properties that only $beforeValidate() declares are still treated as field expressions. #160
  • Behaviour change: insertGraph() throws a ValidationError of type InvalidGraph for #unrelate / #delete markers also with allowMarkers: true, where 3.7.0 skipped the marked models: an insert has nothing to unrelate or delete. allowMarkers stays in the InsertGraphOptions typings until 4.0, deprecated and without effect. #2410
  • Behaviour change: a root model marked with #unrelate or #delete gets the error that the markers can only be used for related models, in upsertGraph() and insertGraph(), also without allowMarkers. #164
  • Behaviour change: upsertGraph() with noUpdate, noDelete, noUnrelate and insertMissing runs as an upsert like any other upsertGraph() call. Models that already exist are left alone instead of failing with duplicate key errors, and markers allowed with allowMarkers take effect. Only insertGraph() runs as an insert.
  • Behaviour change: many-to-many patch() / update() with join table extras select the ids of the matching related rows first, patch the join rows, and then the related rows by these ids, in batches of 500 values. A filter on a column that the patch changes, e.g. .patch({ name: 'renamed', role: 'new' }).where('movies.name', 'old'), left the join rows unchanged. These patches run several statements, so use a transaction if they need to be atomic. #161
  • On MySQL, limit() / offset() of many-to-many patch() / update() with join table extras pick the related rows to update, and only their join rows are updated. Before, all matching join rows were updated. Other databases ignore a limit in updates, as before. #161
  • Behaviour change: asFindQuery() in static hooks always runs the find query and returns the affected rows. It returned the value the query was resolved with, e.g. 0 for a patch with nothing to set, or the value of resolve() / cancelQuery(), and threw TypeError: related.some is not a function for many-to-many patches that only change join table extras.
  • asFindQuery() in static hooks of many-to-many patchAndFetchById() / updateAndFetchById() keeps the query's filters
  • Behaviour change: the id condition of $query() and of patchAndFetchById(), updateAndFetchById() and the other *ById methods gets its own where group, so a user's orWhere() can't get around it: $query().patch(…).orWhere(b) runs id = 1 and (b) instead of id = 1 or b. The where groups are built when the SQL is generated, so asFindQuery() in static hooks no longer drops or repeats where clauses of relation queries, which regressed in 3.3.0. #2191
  • Behaviour change: withGraphJoined() selects the root table's columns instead of *, so the columns of joined tables no longer land on the root models under their plain names. distinct('*') keeps its distinct.
  • distinctOn() with withGraphJoined(), withGraphFetched() and joinRelated(): its columns were treated as selected, so the other columns were missing from the models, and joined relations could fail with "column does not exist".
  • Behaviour change: withGraphJoined() throws when the root query selects a different column under a name it selects itself, e.g. a joined table's column without an alias like select('Person.*', 'pets.id'), select('pets.*'), ref('Person.age').as('name') or 'Person.name as id', and raw SQL, subqueries or aggregates under the name of an id column it needs to build the graph. Before, SQL Server returned arrays of values and the other databases silently kept one of them, sometimes grouping the root models by the wrong id. Raw SQL, subqueries and aggregates under other names replace the column. Select such columns under a different name.
  • Selections are matched after wrapIdentifier mapping (knexSnakeCaseMappers()) for column names and aliases too, so withGraphFetched() no longer selects a relation column a second time and strips it from the result when it's selected under its snake_case name
  • Behaviour change: resultSize() with withGraphJoined() throws model … has no idColumn for models without a primary key if the joined relations include a to-many relation, as the root models can't be counted without ids. Since 3.3.0 it counted the joined rows instead, or failed on MySQL. With only to-one relations, the rows are counted. #164
  • On SQL Server, the order of a relation's modify or an onBuild hook is left out of the subqueries of many-to-many modify queries, withGraphJoined() and joinRelated(), including through.modify, and out of update() / patch() queries, unless they have a limit. SQL Server rejects these orders. Other databases are unchanged. #167
  • On SQL Server, update(), patch() and delete() of aliased tables go through a common table expression, as SQL Server can't alias the table of these statements
  • Behaviour change: on SQL Server, constraint, not-null and data errors are thrown as DBError and its subclasses (UniqueViolationError, ForeignKeyViolationError, NotNullViolationError, CheckViolationError, DataError), with the tedious error as nativeError, like on the other databases. Read err.nativeError.number instead of err.number. This also makes the fix for primary key violations from 3.7.0 work with knex. #2688
  • The warning about duplicate relations in relation expressions like '[a.b, a.c]' says that only the last one is used and that it will throw in objection 4.0, instead of announcing an error in objection 2.0 #164
  • Behaviour change: all warnings objection logs start with [objection] , and each is logged once #164
  • The errors for mixing withGraphJoined() and withGraphFetched() quote relation names with double quotes like other error messages

Docs ​

  • What relate().returning() returns on each database, and which relations accept an array of ids
  • onBuildKnex(): calls on the objection builder that map to knex methods are applied
  • The typings require TypeScript 5.1, and CI checks them with it. With TypeScript 5.0, methods of custom query builders like firstWithName() { return this.where(...).first(); } are implicitly any without a return type annotation.

Other ​

  • The knex peer dependency is >=2.3.0 <4, the versions objection is tested with #95
  • CI runs the integration tests on SQL Server #163

3.7.0 ​

Security ​

  • Behaviour change: the #unrelate and #delete markers need the new allowMarkers: true option in both insertGraph() and upsertGraph(), like #ref needs allowRefs. Without it, a marked model throws a ValidationError of type InvalidGraph, also in insertGraph(), which silently skipped marked models before. The markers come from the graph data and take precedence over the noUnrelate / noDelete options, so graph data from a request body could unrelate or delete related rows that these options were meant to protect. With allowMarkers: true, the markers work as in 3.6.0. #2410

Fixes ​

  • Behaviour change: many-to-many unrelate() / patch() identify the join rows to modify with a correlated EXISTS subquery, instead of row ids (ctid on Postgres, _rowid_ on SQLite), the values of all columns (MySQL) or the related ids (MSSQL). The generated SQL changes. This fixes join tables that are Postgres views, through.modify filters (on Postgres they failed with column "tableoid" does not exist, on SQLite they modified other join rows), MySQL join rows with FLOAT or DATETIME(6) values that don't survive a round trip through JavaScript, and on MSSQL the filters on the join table, which modified all join rows of the matching related rows. On Postgres, SQLite and MSSQL, join rows that only differ in their extras are told apart, so only the matching one changes. On MySQL, the owner and related keys of the matching join rows are fetched with the subquery first, and the join rows are then modified by these keys, as MySQL doesn't let a statement read the table it modifies, e.g. through filters that read the join table or triggers on it. The keys can't tell apart join rows that only differ in their extras, like before 3.3.0. #1853
  • Behaviour change: upsertGraph() upserts the subgraphs of related models that it relates recursively. There, arrays of relation paths in the update and noUpdate options match the full relation path from the root of the graph, like relation expressions since 3.6.0, so they also apply to the root models of the subgraph, e.g. noUpdate: ['movies'] to the related movies. Before, they were matched against the relation paths from the root of the subgraph. The other options have no effect on the root models of a subgraph. #131
  • Behaviour change: runAfter() hooks in the queries of relations loaded with withGraphFetched() no longer see the join columns that objection selects internally, like the other queries since 3.3.0. Hooks that read these columns get undefined. #45
  • A through.modelClass join model with idColumn returning null is no longer changed to use the related join columns as its id #2305
  • Warn once when the joinOperation option is passed to withGraphFetched() or for fetched relations of withGraph(), where it is ignored #2125
  • The warning from 3.4.1 about owner join properties holding arrays also covers many-to-many relations #2667
  • whereInComposite() with multiple columns and a subquery works on SQLite, as a row value comparison, instead of throwing
  • whereInComposite() with multiple columns and a subquery works on MSSQL
  • Many-to-many through.modify filters are kept for model classes bound with bindKnex()
  • insertGraph() / upsertGraph() throw the error about cyclic references for a #ref cycle through owners without a primary key, instead of failing to patch the deferred foreign key by id #2305
  • Behaviour change: with none(), or when a query is resolved with resolve() in a hook, patches and updates no longer modify the extras of many-to-many join rows or the instance of $query(), and BelongsToOneRelation deletes no longer clear the foreign keys of the owners. These were updated although nothing was written. #2184
  • $clone(), toJSON() and validation copy objects with a writable Symbol.toStringTag, e.g. decimal.js numbers, instead of sharing them, which regressed in 3.6.0. Built-ins like URL are still shared, like in lodash's cloneDeep().
  • Patch validation keeps boolean subschemas like properties: { b: false }. They were dropped, so patches passed that the schema forbids.
  • Patch validation no longer validates field expression keys with numeric parts like 'tags:0' against object schemas, which rejected patches of array elements
  • Required properties given as query properties, e.g. raw(), failed validation with an error about a missing schema when the schema uses $ref to the schema of another model class #2199
  • On Postgres, a backslash in front of a ? in a JSON field expression key is kept, which knex dropped #2077
  • On MySQL, subqueries of the updated or deleted table are also wrapped in where groups and join clauses, which failed with ER_UPDATE_TABLE_USED
  • resultSize() and the total of page() with withGraphJoined() keep the selections that groupBy() and having() refer to by alias #2329
  • Relation expressions no longer mistake inherited object properties for relations, e.g. when merging graph expressions or parsing a relation named constructor #63
  • $clone() no longer prints Node's Buffer() deprecation warning (DEP0005)

Types ​

  • fetchGraph<Person>() with only the model type argument compiles again, without narrowing
  • PartialModelObject accepts partial objects for nullable to-one relations #2190
  • Behaviour change: snakeCaseMappers() and knexSnakeCaseMappers() take separate option types, SnakeCaseMappersOptions and KnexSnakeCaseMappersOptions, with preserveJsonKeys only on the former, so knexSnakeCaseMappers({ preserveJsonKeys: true }) no longer compiles. The option never had an effect there. The shared options are SnakeCaseOptions.
  • The rows of relate().returning() are typed as any, and returning() accepts multiple columns #2044

Docs ​

  • Graph option expressions can't name a relation together with relations below it, or repeat a relation like '[movies.reviews, movies.actors]' #131
  • Shared validators throw for different schemas with the same $id
  • The typings require TypeScript 5.0 since 3.3.0
  • Workarounds for the withGraph narrowing with reassignments and generic helpers
  • insert() and patch() ignore relation data

Other ​

  • Behaviour change: remove the mapNestedKeys option of knexSnakeCaseMappers() and knexIdentifierMapping() that 3.5.0 added. It also converted the keys of JSON columns that the driver returns parsed. Results are converted one level deep again, like before 3.5.0. The KnexIdentifierMappingOptions type is removed too. #2273

3.6.0 ​

What's new ​

  • The graph options of insertGraph() / upsertGraph() that take relation paths (relate, unrelate, insertMissing, update, noInsert, noUpdate, noDelete, noRelate, noUnrelate) also accept a relation expression in the same syntax as withGraphFetched(), e.g. noInsert: '*' or noDelete: '[pets, movies.reviews]'. Like the array, an expression only matches the relations at the end of the paths it names, so 'movies.reviews' doesn't match movies. '*' matches all relations at any depth, and recursive expressions like 'parent.^' every level of the recursion. Root models are never matched, so noInsert: '*' inserts new root models but no related ones. Aliases and empty expressions throw an error. #131

Fixes ​

  • upsertGraph() patches the related models of many-to-many relations by their id, and their extra properties in the join row by the ids of both ends. Until now these patches filtered by a subquery that joins the join table, and on MySQL, the patches of a graph upserted without a transaction deadlocked each other. The SQL of these patches changes. #135
  • The warning from 3.4.0 about owner models missing a join property skips the foreign key of a BelongsToOneRelation, unless it's part of the id. It's also missing when it was never set, e.g. on models returned by insert() or created with fromJson(), where no related model is the right result. A foreign key left out of a partial select() looks the same and no longer warns either. Join properties of all other relations still warn, including non-id columns. #132
  • relate(), $relatedQuery().insert() and insertGraph() / upsertGraph() inserting or relating models of many-to-many and has-one-through relations whose join table is also the related table throw an error, like unrelate() since 3.4.0. They inserted the join rows as new related rows, or failed with unique violations after inserting the related rows. #133
  • Recursive upserts of related models with children no longer warn about the unknown graph option options since 3.4.0

Other ​

  • Replace the custom build of lodash's cloneDeep() from 2018 with a small implementation, which $clone(), toJSON() and validation use to copy objects in model properties. It copies model data the same way, 2 to 4 times faster. Objects with a null prototype keep it instead of getting Object.prototype, BigInt64Array / BigUint64Array are copied instead of shared, and so are Buffers nested in objects, which shared their memory with the original before.
  • The parsers of relation expressions and JSON field expressions are generated with Peggy instead of PEG.js 0.10, with npm run build:parsers. Parse results and error messages are the same.
  • The example projects use current versions of their dependencies and need Node.js 20.17 or newer

3.5.0 ​

What's new ​

  • Add the mapNestedKeys option to knexSnakeCaseMappers() and knexIdentifierMapping(), which also converts the keys of objects nested one level down, e.g. the column names in the rows of knex's nestTables: true option on MySQL: knexSnakeCaseMappers({ mapNestedKeys: true }) #2273
  • The relation mapping's beforeInsert(model, context, owner) hook receives the owner model as a third argument. With insertGraph() / upsertGraph() it's the parent object from the input graph, which may not be inserted yet, and it's undefined when the query was started from ids or several owners. #2240

Types ​

  • Add the opt-in TypedRelationMappings<M> helper: used with satisfies on relationMappings, it checks that the mappings use relation properties of the model, a relation type matching their cardinality, and the right modelClass #16
  • Add Generated<T> to mark columns the database fills in, e.g. id!: Generated<number>, and the Insertable<M> / InsertableGraph<M> types for insert data, which leave them optional #16
  • Add isOneToOne() to the Relation typings and docs. It's the safe way to check for relations to a single model, as HasOneRelation and HasOneThroughRelation extend the many-relations.

3.4.1 ​

Fixes ​

  • Select aliased aggregates like count('* as n'), count('id', { as: 'n' }) and count({ n: 'id' }) in withGraphJoined() modifiers. They were treated as selecting all columns, or were missing from the result. Unaliased aggregates are left out, since their column name differs between databases. #2219
  • JSON field expressions (ref('col:path'), 'col:path' keys in patches) and the whereJson*() methods warn once on databases other than Postgres, where they generate invalid or silently wrong SQL. MySQL, for example, reads #> as a comment. On MySQL, whereJsonSupersetOf(column, value) / whereJsonSubsetOf(column, value) with a plain column and a JSON object or array are passed on to knex, which supports them there. #276
  • Fix the infinite recursion in andWhereJsonNotSupersetOf()
  • The warnings about missing join properties from 3.4.0 also cover ManyToManyRelation and HasOneThroughRelation: owner models missing a join property, and related models that come back without the owner join column, e.g. stripped by $parseDatabaseJson(). #1832, #2258
  • Warn once when a relation's owner join property holds an array, e.g. a JSON array of keys, which isn't supported and silently fetched no related models. Use a ManyToManyRelation through a join table instead. #2667

3.4.0 ​

What's new ​

  • Support models without a primary key: return null from idColumn, e.g. for join table models. Methods that need an identifier, like findById(), deleteById() and $query(), throw an error. #2305

Fixes ​

  • insertGraph() / upsertGraph() with relate: true silently dropped HasManyRelation / HasOneRelation children whose composite id contains the foreign key, and deleted the existing ones without inserting them again #2544
  • Required properties given as query properties, e.g. raw(), val(), ref() or subqueries, failed validation #2199
  • upsertGraph() treated JSON arrays and objects as unchanged when they loosely equal the current value, e.g. false and []. Behaviour change: these values are now patched. #1874
  • Many-to-many and has-one-through relations whose join table is also the related table failed with SQL errors in withGraphFetched(), $relatedQuery() and relatedQuery(). unrelate() and patching join table extras on such relations throw an error instead of modifying the wrong rows. #1803
  • Patch validation rejected valid patches when an anyOf / oneOf option only consisted of required, which 3.3.0 didn't cover yet #1681
  • Throw UniqueViolationError for primary key violations on MSSQL #2688
  • $fetchGraph() / fetchGraph() / $relatedQuery() warn once when owner models are missing a join property, e.g. an id left out of a partial select(). The relation silently came back empty. Since 3.6.0, the foreign keys of BelongsToOneRelation owners no longer warn, see #132. #1832
  • withGraphJoined() checks column aliases against the identifier length limit of the database (MySQL 256, MSSQL 128, SQLite none) instead of always the 63 of Postgres, and the error points to the minimize option. The limit isn't configurable. #2242
  • withGraphFetched() / $fetchGraph() warn once when related models are missing a join property, e.g. snake_case relation mappings with knexSnakeCaseMappers(). They were silently dropped. #2258
  • insertGraph() / upsertGraph() warn once per unknown option, e.g. a misspelled noInset. The option is ignored, and will throw in 4.0. #84

Types ​

  • Add the only option to from(), table() and into(), e.g. for Postgres ONLY #2182

Docs ​

  • first() doesn't limit the related items per parent in modifyGraph() #1647
  • runAfter() in the queries of fetched relations can't replace the related models #2647
  • page() and range() need an orderBy() with a unique tiebreaker to avoid repeated or skipped rows #2575
  • Typing model properties with a merged interface #2237
  • The through mapping of relations whose join table is also the related table #1803

Other ​

  • Breaking for old knex: the knex peer dependency is >=2.3.0 instead of >=1.0.1. Some fixes of 3.3.0 don't work with older knex versions. CI now also tests knex 2.3.0. Part of #95
  • Tests no longer use bluebird, lodash and uuid

3.3.0 ​

Security ​

  • Escape keys in JSON field expressions. A ? in a key used in ref(), a patch or update with field expression keys, or the whereJson* methods was read by knex as a binding placeholder #2077, and a ' broke out of the SQL string literal, allowing SQL injection when keys come from user input.

What's new ​

  • withGraphFetched() / withGraphJoined() narrow the result types: fetched relations become required on the result. Generic helpers with an explicit QB return type need withGraphFetched<string>(). #25 Behaviour change: this also breaks variables declared with let that are later assigned an unnarrowed query, helpers generic over the model type that return QueryBuilder<M>, and explicit type arguments like Person.fetchGraph<Person>() (fixed in 3.7.0). Annotate the variable, or use withGraphFetched<string>().
  • Add withGraph() with algorithm-agnostic merging, and isJoinChildQuery() #63
  • Add patchById() and updateById() #1415
  • insertGraph() / upsertGraph() resolve cyclic #ref dependencies by deferring BelongsToOne foreign keys. Graphs that threw before now succeed, and the deferred foreign keys are written with one patch() per owner model, which runs its update hooks. #1482
  • Add a preserveJsonKeys option to snakeCaseMappers() to map only the column part of field expressions #1089
  • Support empty keys in JSON field expressions, e.g. col:[""] #2680. Behaviour change: col:[""] and col:[''] used to refer to a key consisting of the two quote characters, col:[a,b] to the path a.b instead of the key a,b, and spaces around keys in brackets were trimmed.
  • whereJson* methods accept ref(), val(), raw() and subqueries on the right side
  • Pass returning() options, e.g. includeTriggerModifications, on to knex #2309

Fixes ​

  • Group the user's where clauses when adding a relation's owner condition. $relatedQuery(...).where(a).orWhere(b).delete() / patch() affected other owners' rows, and finds returned them. #2191, #1909
  • findByIds([]) / whereInComposite() with an empty array and composite ids produced invalid SQL, and matched every row on SQLite #1914
  • onConflict().ignore(): inserted rows were assigned to the wrong models, $setDatabaseJson could crash, and insertAndFetch() fetched undefined ids. Behaviour change: on SQLite this uses RETURNING, which needs SQLite 3.35 or newer, and it throws when it can't match the returned rows to the models, e.g. with ids generated by the database. Ignored models are still returned, with their input values. #2320, #2597, #2661
  • asFindQuery() in many-to-many update and delete hooks lost its filters #2266
  • Patch validation: not schemas stay intact #1681, and nested required is also stripped for nullable and untyped objects, extending the 3.1.2 fix #1664
  • resultSize() and the total of page() count root models instead of joined rows with withGraphJoined() #2143, #2329. The limit of page() still applies to the joined rows.
  • Keep aliased subquery and raw selections in withGraphJoined() modifiers #2365
  • Map column names in whereJson* methods with knexSnakeCaseMappers. Behaviour change: column names given in database format are mapped again, which changes some of them, e.g. data_2 with underscoreBeforeDigits.
  • Match table.* selections through knex identifier mapping #2288
  • Use the keys of modified graphExpressionObject() results #2793
  • Track internally selected columns on the query builder, so runAfter() hooks of the query never see them. The queries of relations loaded with withGraphFetched() still do. #45
  • Behaviour change: many-to-many unrelate() / patch() only modify the join rows matching the filters, not all join rows of the matching related rows #1853. The generated SQL of these operations changes. On MySQL, the matching join rows are fetched in a separate query, which avoids ER_UPDATE_TABLE_USED ("You can't specify target table … for update in FROM clause") #2127 and ER_CANT_UPDATE_USED_TABLE_IN_SF_OR_TRG when a trigger on the join table modifies the related table #2406, and unrelate() returns 0 instead of [] when nothing matches. Ported from #2406.
  • Behaviour change: asFindQuery() in hooks no longer applies the runAfter() callbacks of the original query, including those of throwIfNotFound() and traverse(), and returns [] instead of [undefined] for a findById() that finds nothing #2093
  • Behaviour change: field expression keys in patch objects, e.g. 'meta:a.b', are validated against the nested schema of the JSON column. Patches like { 'meta:b': value } no longer fail with additionalProperties: false, but patches that passed before can fail the nested schema #1666
  • Behaviour change: the joinOperation option of withGraphJoined() applies per call, no longer to all relations of the query #2125
  • Behaviour change: withGraphJoined() throws when it can't identify rows, instead of silently merging them #2737. It still needs a primary key on every model, and models without one throw a clear error #2748.
  • insertGraph() / upsertGraph() warn when onConflict(), ignore() or merge() is used. The clause is ignored, and will throw in 4.0. #2156

Types ​

  • Breaking for TypeScript < 5.0: the typings use const type parameters, so they need TypeScript 5.0 or newer. skipLibCheck doesn't help, as older versions can't parse them.
  • Export the db-errors classes as types #2499

Docs ​

  • The docs moved to VitePress and are hosted at ditojs.github.io/objection
  • Document relatedFindQueryMutates / relatedInsertQueryMutates #2356
  • Document running code after a transaction commits #2582

Other ​

  • Update knex to 3.3 in development, which fixes inverted bindings in delete() with joinRelated() #2799

Thanks to @falkenhawk and Marcin L for the many-to-many work in #2406, @kapouer for #2680 and @cesumilo for #2625, which led to preserveJsonKeys.

3.2.0 ​

Objection is now maintained at ditojs/objection.

What's new ​

  • Support mixing withGraphJoined() and withGraphFetched() in the same query. Before, the last method called silently decided the algorithm for all relations. A top-level relation can only be loaded with one of them. Behaviour change: loading the same top-level relation with both, e.g. withGraphFetched('pets').withGraphJoined('pets.toys'), throws. Before, all relations were joined. #2269
  • Add none() to make a query match no rows #2184
  • upsertGraph(): mark individual related models with #unrelate or #delete #2410. Since 3.7.0, the markers need the allowMarkers: true option.
  • Return rows from relate() with returning() #2044
  • Apply select() to the fetch query of updateAndFetch(), patchAndFetch() and their ById variants #1938
  • Add a noDoubleUnderscores option to the snake case mappers #2805
  • Pass format options from toJSON() / $toJson() to $formatJson() of the model and all nested models #2033
  • Support $defs in JSON schemas #2576

Fixes ​

  • Behaviour change: throw when for() is used outside of relatedQuery(). It was silently ignored before, so Model.query().for(1).delete() deleted every row of the table. #2185
  • Behaviour change: throw when for() is called after a write method on a relatedQuery(). Before, update(), patch() and delete() silently did nothing, and insert() and relate() wrote NULL foreign keys.
  • Fix validators being shared between models with the same uniqueTag() but different schemas. Part of #2540
  • Fix orderBy() with refs and raws as array items #2252
  • Clear an orderBy added at build time, e.g. by a relation's modify, from the resultSize() and page() count query #2747
  • Support Model.ref() references in relation join mappings #1873
  • Reference the bare column in JSON where methods when no JSON path is given, so Postgres can use GIN indexes #2008
  • Fix subqueries in join builders on MySQL (parentQuery.isUpdate is not a function) #2407
  • Make union() deduplicate regardless of array size #2811
  • Serialize relation modifiers and aliases before the recursion marker in RelationExpression.toString() #2812

Types ​

  • Fix first() infinite type recursion in custom query builder methods #2637
  • PartialModelObject accepts partial model data for nested relations, so inserting objects typed with data-only interfaces type-checks #2190
  • Keep custom query builder types after throwIfNotFound()
  • Add a QueryBuilder constructor typing for custom query builders #2306
  • Allow null and readonly arrays for idColumn, readonly arrays for jsonAttributes. Code that reads them needs to handle these types. Models without a primary key are supported at runtime since 3.4.0. #2693, #2745
  • Allow bigint values in where methods #2318
  • Support object joins, limit() / offset() with skipBinding, and aliased aggregates. Part of #2189
  • Fix the tableNameFor(), tableRefFor() and aliasFor() typings #2790
  • Add knexIdentifierMapping #1947
  • Add tableName(), tableRef() and emptyInstance() to QueryBuilder #2792, #2794
  • Add the column list overload of with() #2749
  • Fix generic static this for query(), fromJson() and fetchGraph() #2700

Docs ​

  • Document what afterInsert() receives as result #2342
  • Document that withGraphJoined() requires a primary key #2748
  • Document the skipUndefined() deprecation #2149
  • Use declare for the query builder type properties in the custom query builder and plugin recipes, which fail with TS2612 on ES2022+ targets
  • Fix the koa-ts example typings #2361
  • Clarify whereNotColumn() #2808

Other ​

  • Drop Node 14 from CI, since knex 3 requires Node 16+. Objection's engines still include Node 14, but it's no longer tested.

Thanks to everyone whose pull requests made it into this release: @amit-meshbey, @androvonx95, @bilalakbar, @Bolt4243, @cesumilo, @falkenhawk, @geeksilva97, @IlyaSemenov, @kapouer, @kartikdp, @LiahMartens, @max-kahnt-keylight, @nickbouldien, @nickfuryoc, @ralcorta and @salisbury-espinosa.

3.1.5 ​

What's new ​

  • Types: Fix generic static this #2533
  • Types: Add fromRaw to `FromSelector regex #2628
  • Types: Fix argument types of onConflict() #2635
  • Types: Make trx optional for Model.transaction(trx, cb) #2694

3.1.4 ​

What's new ​

  • Fix upsertGraph() $beforeUpdate() calls on empty relates #2605
  • Don't call onError() with internal exceptions #2603
  • Remove docs and typings for nonexistent $pick()
  • Make $omitFromJson() + $omitFromDatabaseJson() compatible with old $omit() syntax

3.1.3 ​

What's new ​

  • Revert generic constructor type change #2531, #2399

What's new ​

  • Patch Validator: Prevent recursion on inner properties #2520

3.1.2 ​

What's new ​

  • Patch Validator: Prevent recursion on inner properties #2520

3.1.1 ​

What's new ​

  • Only add Ajv formats if they weren't added in user-land already #2482

3.1.0 ​

What's new ​

  • Support $beforeUpdate() mutations in upsertGraph() #2233
  • Remove deprecated $afterGet() hook #2477
  • Drop support for Node v12 #2478

3.0.5 ​

What's new ​

3.0.4 ​

What's new ​

3.0.3 ​

What's new ​

3.0.2 ​

What's new ​

3.0.1 ​

What's new ​

3.0.0 ​

What's new ​

Breaking changes ​

See the migration guide.

2.2.10 ​

What's new ​

  • Add modelClass property for ValidationError and NotFoundError.

2.2.9 ​

What's new ​

  • Add noWait query builder method.

2.2.8 ​

What's new ​

2.2.7 ​

What's new ​

  • QueryBuilder.castTo can now be used to cast query results to any typescript type.

2.2.6 ​

What's new ​

2.2.5 ​

What's new ​

2.2.4 ​

What's new ​

  • Add support for onConflict, merge and ignore knex methods.

2.2.2 ​

What's new ​

2.2.1 ​

What's new ​

2.2.0 ​

What's new ​

2.1.4 ​

What's new ​

2.1.3 ​

What's new ​

  • Add underscoreBetweenUppercaseLetters option for snake case mappers. #1676

2.1.2 ​

What's new ​

  • Fix startTransaction typings.

2.1.1 ​

What's new ​

2.1.0 ​

What's new ​

Changelog ​

2.0.10 ​

What's new ​

2.0.9 ​

What's new ​

2.0.8 ​

What's new ​

2.0.7 ​

What's new ​

2.0.6 ​

What's new ​

2.0.5 ​

What's new ​

  • Fixes upsertGraph bug where composite keys were not selected correctly. See the fix here.

2.0.4 ​

What's new ​

  • New skipFetched option for fetchGraph and $fetchGraph

2.0.3 ​

What's new ​

2.0.0 ​

What's new ​

  • Cleaner and more consistent API. A lot of methods have been renamed, removed combined and cleaned up. Most of the old methods still exist, but print a deprecation warning when first used. Some examples:

    • eager -> withGraphFetched
    • joinEager -> withGraphJoined
    • removed eagerAlgorithm (you must explicitly use either withGraphFetched or withGraphJoined)
    • merged allowEager, allowInsert and allowUpsert into one method allowGraph
    • $loadRelated -> $fetchGraph
    • joinRelation -> joinRelated
    • $relatedQuery no longer mutates the receiving model instances
  • New static hook API. The old instance hooks are still around.

  • relatedQuery can now be used for more than just subqueries. See the examples here.

  • modifiers can now take arguments and are a lot more useful. See this recipe for more info.

  • Objection now uses the db-errors library by default to wrap the database errors.

  • insertMissing upsertGraph option now works as expected with relate: true: items that are not found in the database are inserted.

  • Brand new typings written from scratch with many improvements and finally a support for custom query builders

  • A bunch of improvements and bug fixes for upsertGraph, including a huge speedup in some cases due to less data fetching.

  • A brand new fn helper for calling SQL functions.

  • Objection now uses native promises instead of bluebird.

  • Objection is now leaner as we dropped a bunch of dependencies like bluebird and lodash.

  • In addition to all of this, a huge number of bugs has been squashed!

Breaking changes ​

See the migration guide.

1.6.10 ​

1.6.9 ​

  • Revert fix for #1089. It was causing more bugs than it fixed. #1089 will be addressed in 2.0.
  • Typings updates

1.6.8 ​

1.6.7 ​

  • A bunch of regression bug fixes.

1.6.3 ​

1.6.2 ​

  • Add as method for raw making it possible to use raw expressions in joinEager modifiers (as long as you give names to your raw expressions using as).

1.6.1 ​

  • Fix some very rare upsertGraph edge cases.

1.6.0 ​

  • Add Model.traverseAsync and modelInstance.$traverseAsync methods.

  • Fixes: #842 and #1205. This bug is about subqueries "inheriting" parent query table name and alias. This bug has been around a long time and there is a small chance that people have started accidentally or on purpose use it as a feature. If you get weird reference errors from subqueries (relation not found, table not found etc.) you may need to explicitly give an alias or use from in your subqueries after this update. This is a borderline breaking change, but since 2.0 is still pretty far away, I wanted to get this out faster. If I'm wrong and people are heavily depending on this bug, I'll revert the change.

  • Fixes: #1215

  • Fixes: #1206

1.5.3 ​

What's new ​

1.5.1 ​

What's new ​

  • Relations are now loaded lazily #1202
  • relationMappings.modelClass can now be a function that returns a model class.

1.5.0 ​

What's new ​

  • fix #1131
  • fix #1114
  • fix #1185
  • fix #1109
  • fix #1110
  • add eagerObject and eagerModifiers accessors to QueryBuilder.
  • complete rewrite of insertGraph and upsertGraph code. The rewrite brought a bunch of small performance optimizations and makes future development easier. No breaking changes.
  • Chaining returning('*') to insertGraph or upsertGraph now propagates the call to all insert, update and delete operations.
  • Code using objectio can now be transpilsed to ES5. No need to add babel workarounds anymore.

1.4.0 ​

What's new ​

1.3.0 ​

What's new ​

  • Use objection.raw instead of knex.raw in Model.raw. #1077
  • Allow modifiers (namedFilters) to be used in modifyEager too.
  • Add underscoreBeforeDigits option for snake case converters. #1025
  • fix #1074
  • Typing fixes

1.2.3 ​

What's new ​

1.2.2 ​

What's new ​

  • Improve reference cycle detection in upsertGraph

1.2.1 ​

What's new ​

1.2.0 ​

What's new ​

  • fix #919
  • fix #964
  • Add aliasFor method to public API
  • Prevent bluebird warnings
  • UPPER_SNAKE_CASE support for snakeCaseMappers and knexSnakeCaseMappers

1.1.10 ​

What's new ​

  • Nothing! the npm release was somehow borked. This was just a rerelease of 1.1.9.

1.1.9 ​

What's new ​

1.1.8 ​

What's new ​

1.1.7 ​

What's new ​

1.1.6 ​

What's new ​

  • Add typings for fetchTableMetadata, tableMetadata and onbuildknex

1.1.5 ​

What's new ​

1.1.4 ​

What's new ​

  • fix subquery bug causing incompatibility with knex 0.14.5 and sqlite3

1.1.3 ​

What's new ​

  • fix regression in 1.1.2 (sorry about this) #869

1.1.2 ​

What's new ​

  • Add virtuals option for toJSON and $toJson #866
  • fix #868

1.1.1 ​

What's new ​

  • fix #865
  • fix bug where the static Model.relatedQuery didn't use the relation name as an alias for the table. This may break code if you have explicitly referenced the subquery table. #859

1.1.0 ​

What's new ​

1.0.1 ​

What's new ​

  • Added public Relation.joinModelClass accessor
  • Don't call returning on sqlite (prevents a warning message added in knex 0.14.4)
  • fix #844
  • Small documentation updates
  • Small typing fixes end updates

1.0.0 🎉 ​

What's new ​

Breaking changes ​

  • modelInstance.$query().delete().returning(something) now returns a single instance instead of an array. #659

  • Node 6.0.0 is now the minimum. Objection will not work on node < 6.0.0.

  • ValidationError overhaul. This is a big one, so read this carefully! There are three things to check when you migrate to 1.0:

    1. The createValidationError and ValidationError interfaces have changed. If you have overridden the createValidationError method in your project, or you create custom ValidationError instances you need migrate to the interfaces.
    2. The model validation errors (jsonSchema violations) have remained pretty much the same but there are couple of differences. Before, the keys of error.data were property names even when a nested object in a graph failed a validation. Now the keys for nested validation errors are key paths like foo.bar[2].spam. Another tiny difference is the order of validation errors for each key in error.data. Let's say a property spam failed for your model and error.data.spam contains an array of objects that describe the failures. Before, the first failed validation was the last item in the array, now it is the first item.
    3. All ValidationErrors now have a type field. Before all ValidationErrors but the model validation errors (errors like "invalid relation expression", or "cyclic model graph") had no type, and could only be identified based on the existence of some weird key in error.data. The error.data is now removed from those errors and the type should be used instead. The message from the data is now stored in error.message.
  • Removed deprecated methods whereRef, whereJsonField and whereJsonEquals. The ref helper can be used to replace the whereRef calls. ref and lit can be used to replace the removed json methods.

  • ManyToMany extras now work consistently in queries and filters. #760. This is not a breaking change per se, but can cause some queries to fail with a "ambiguous identifier" error because the join table is now joined in places where it previously wasn't. You need to explicitly specify the table for those failing columns using Table.theColumn syntax.

Changes ​

  • isFindQuery is renamed to isFind and deprecated.

0.9.4 ​

What's new ​

0.9.3 ​

What's new ​

0.9.2 ​

What's new ​

  • Fix regression: from fails with a subquery.

0.9.1 ​

What's new ​

0.9.0 ​

What's new ​

  • Relations can now be defined using keys inside JSON columns. See the examples here.
  • lit helper function #275
  • Fixes for upsertGraph when using composite keys. #517
  • Added noDelete, noUpdate, noInsert, noRelate and noUnrelate options for upsertGraph. See UpsertGraphOptions docs for more info.
  • insertGraph now accepts an options object just like upsertGraph. relate option can be used instead of #dbRef. #586

Breaking changes ​

  • Instance update/patch with returning now return a single object instead of an array. #423

  • Because of the support for JSON relations the Relation class has changed a bit.

0.8.8 ​

What's new ​

  • Typing updates: #489 #487
  • Improve resultSize method. #213
  • Avoid unnecessary updates in upsertGraph #480

0.8.7 ​

What's new ​

  • throwIfNotFound now also throws when update or delete doesn't change any rows.
  • mixin and compose helpers for applying multiple plugins. #475 #473
  • Typing updates #474 #479
  • upsertGraph now validates patched models correctly. #477

0.8.6 ​

What's new ​

  • Finally: the first version of upsertGraph method! Please open issues about bugs, WTFs and missing features.
  • Strip readonly virtual properties in fromJson & friends #432
  • Fixed #439

0.8.5 ​

What's new ​

0.8.4 ​

What's new ​

  • New shorthand methods joinEager, naiveEager, mergeJoinEager and mergeNaiveEager.
  • New shorthand method findOne
  • New reflection method isFindQuery
  • ManyToMany extra properties can now be updated #413

0.8.3 ​

What's new ​

0.8.2 ​

What's new ​

  • Model.namedFilters object for defining shared filters that can be used by name in eager expressions.
  • Full support for views and table aliases in eager, join, joinRelation etc. #181
  • Fix bindTransaction bug with ManyToManyRelation junction tables #395

0.8.1 ​

What's new ​

  • throwIfNotFound method for making empty query results throw an exception.
  • fix error when passing model instance to a where method. #387

0.8.0 ​

What's new ​

  • All query methods now call Model.query to create a QueryBuilder instance #346
  • Objection is no longer transpiled. One of the implications is that you can use a github link in package.json to test experimental versions.
  • count can now be called without arguments #364
  • A new getRelations method for plugin development and other reflection greatness.

Breaking changes ​

Old model definition

js
function Person() {
  Model.apply(this, arguments);
}

Model.extend(Person);

Person.tableName = 'Person';

Person.prototype.fullName = function () {
  return this.firstName + ' ' + this.lastName;
};

// More static and prototype methods.

Easiest way to migrate to class and extends keywords

js
class Person extends Model {}

Person.tableName = 'Person';

Person.prototype.fullName = function () {
  return this.firstName + ' ' + this.lastName;
};

// More static and prototype methods.
  • Support for node versions below 4.0.0 has been removed. With it the support for legacy class inheritance using Model.extend method has also been removed. This means that you need to change your model definitions to use the class and extends keywords. To achieve this with the minimum amount of changes you can simply swap the constructor function and Model.extend to a class definition. You can still define all static and prototype methods and properties the old way. See the example on the right -->

    Note that this also affects Babel transpilation. You cannot (or need to) use babel-plugin-transform-es2015-classes anymore. See the ESNext example project as an example of how to setup babel.

  • The default value of pickJsonSchemaProperties was changed to false. Before, all properties that were not listed in jsonSchema were removed before insert, patch or update (if jsonSchma was defined). Starting from this version you need to explicitly set the value to true. You may have been used this feature by accident. If you have weird problems after the update, try setting objection.Model.pickJsonSchemaProperties = true; to see if it helps.

  • relate and unrelate methods now return the result of the underlying query (patch in case of HasManyRelation, HasOneRelation, and BelongsToOneRelation. insert otherwise). Before the method input was always returned.

  • Model.RelatedQueryBuilder is removed. Model.QueryBuilder is now used to create all query builders for the model. This only affects you if you have defined custom query builders.

0.7.12 ​

What's new ​

0.7.11 ​

What's new ​

0.7.10 ​

What's new ​

  • fix bugs that prevented using $relatedQuery and eager together with JoinEagerAlgorithm
  • typing updates

0.7.9 ​

What's new ​

0.7.6 ​

What's new ​

  • range and page methods now use a window function and only generate one query on postgresql #62
  • fix MSSQL 2100 parameter limit in eager queries #311

0.7.5 ​

What's new ​

0.7.4 ​

What's new ​

  • automatically select columns needed for relations #309
  • fix an issue where $formatJson was called inside insertGraph #326

0.7.3 ​

What's new ​

  • fix #325
  • fix an issue where select had to be used in addition to distinct in some cases

0.7.2 ​

What's new ​

  • HasOneThroughRelation relation type.

0.7.1 ​

What's new ​

  • fix JoinEagerAlgorithm NPE bug

0.7.0 ​

What's new ​

  • jsonSchema without properties now works. #205
  • relationMappings can now be a function. #227
  • many to many extras can now be aliased. #223
  • zero values are now allowed in relation columns. #228
  • active transaction can now be accessed in $before/$after hooks through queryContext.transaction property.
  • Validation can now be easily modified through a new Validator interface. #241 #199
  • fix a JoinEager problem where an empty result for a relation caused the following relations to be empty. #292
  • ref(fieldExpression) syntax to reduce need for knex.raw and updating single attribute inside JSON column. #270
  • mergeEager method.

Breaking changes ​

  • $relatedQuery now returns a single model instead of an array for belongsToOne and hasOne relations. #155
  • identifier of a model can now be updated. Be careful with this one! Before if you forgot a wrong id in an update/patch operation, it would simply get ignored. Now the id is also updated just like any other column #100
  • Table.* is now selected by default in all queries instead of *. This will break some join queries that don't have an explicit select clause. #161
  • ValidationError.data is now an object including, for each key, a list of errors with context info. #283

0.6.2 ​

What's new ​

  • relationMappings can now be a function #227

0.6.1 ​

What's new ​

0.6.0 ​

What's new ​

  • Eager loading can now be done using joins and zero extra queries. See eagerAlgorithm, defaultEagerAlgorithm and eager for more info.
  • #ref in graph inserts can now contain extra properties for many-to-many relations #156
  • #dbRef can now be used to refer to existing rows from a insertWithRelated graph.
  • modelPaths attribute for cleaner way to point to models in relationMappings.
  • pickJsonSchemaProperties config parameter #110
  • insertGraphAndFetch with insertWithRelatedAndFetch alias. #172
  • Added $beforeDelete and $afterDelete hooks #112
  • Old values can now be accessed from $beforeUpdate, $afterUpdate, $beforeValidate and $afterValidate hooks #185
  • Support length property #168
  • Make sure operations are executed in the order they are called #180
  • Fetch nothing if the where clauses hit no rows in update/patchAndFetchById methods #189
  • Lots of performance tweaks.
  • $loadRelated and loadRelated now return a QueryBuilder.

Breaking changes ​

  • Undefined values as query method arguments now throw an exception. Before they were just silently ignored and for example delete().where('id', undefined) caused the entire table to be deleted. skipUndefined method can be called for a query builder to handle the undefined values the old way.

  • Deprecated method dumpSql is now removed.

  • $loadRelated and loadRelated now return a QueryBuilder. This may break your code is some rare cases where you have called a non-standard promise method like reflect for the return value of these functions.

0.5.5 ​

What's new ​

0.5.4 ​

What's new ​

  • bugfix: insertWithRelated now works with additionalProperties = false in jsonSchema
  • Add updateAndFetch and patchAndFetch methods for $query
  • bugfix: afterGet was not called for nested models in eager query
  • Use ajv instad of tv4 for json schema validation

0.5.3 ​

What's new ​

  • ES6 promise compatibility fixes.

0.5.1 ​

What's new ​

  • $afterGet hook.

0.5.0 ​

What's new ​

  • joinRelation family of query builder methods.
  • HasOneRelation for creating inverse one-to-one relations.
  • Relations have been renamed OneToOneRelation --> BelongsToOneRelation, OneToManyRelation --> HasManyRelation. The old names work, but have been deprecated.
  • withSchema now works as expected and sets the schema of all queries executed by the query builder the method is called for.
  • filterEager method for better eager query filtering.
  • extra properties feature for selecting/inserting columns from/to the join table in many-to-many relations.
  • Eager query recursion depth can be controlled like so: parent.^5.

0.4.0 ​

What's new ​

Breaking changes ​

There shouldn't be any major breaking changes. We moved from ES5 to ES7 + babel in this version so there are big changes in the codebase. If something comes up, please open an issue.

There are a few known corner cases that may break:

  • You can now define a model for the join table of ManyToMany relations in relationMappings. This is optional, but may be needed if you already have a model for a ManyToMany relation and you use snake_case to camelCase conversion for the column names. See the documentation on the through property of relationMappings.

  • The repo no longer contains the actual built javascript. Only the ES7 code that is transpiled when the code is published to npm. Therefore you can no longer specify a git hash to package.json to use for example the HEAD version. We will start to publish alpha and RC versions to npm when something new and experimental is added to the library.

0.3.3 ​

What's new ​

  • fix regression: QueryBuilder.from is broken.

0.3.2 ​

What's new ​

  • Improved relation expression whitespace handling.

0.3.1 ​

What's new ​

  • whereJson* methods can now be used inside functions given to where methods.
  • Added multiple missing knex methods to QueryBuilder.

0.3.0 ​

What's new ​

Breaking changes ​

  • QueryBuilder methods update, patch and delete now return the number of affected rows. The new methods updateAndFetchById and patchAndFetchById may help with the migration
  • modelInstance.$query() instance method now returns a single model instead of an array
  • Removed Model.generateId() method. $beforeInsert can be used instead

0.2.8 ​

What's new ​

  • ES6 inheritance support
  • generator function support for transactions
  • traverse,pick and omit methods for Model and QueryBuilder
  • bugfix: issue #38

0.2.7 ​

What's new ​

  • bugfix: fix #37 also for $query().
  • Significant toJson/fromJson performance boost.

0.2.6 ​

What's new ​

  • bugfix: fix regression bug that broke dumpSql.

0.2.5 ​

What's new ​

  • bugfix: fix regression bug that prevented values assigned to this in $before callbacks from getting into the actual database query

0.2.4 ​

What's new ​

  • bugfix: many-to-many relations didn't work correctly with a snake_case to camelCase conversion in the related model class.

0.2.3 ​

What's new ​

  • Promise constructor is now exposed through require('objection').Promise.

0.2.2 ​

What's new ​

  • $beforeUpdate, $afterUpdate, $beforeInsert etc. are now asynchronous and you can return promises from them.
  • Added Model.fn() shortcut to knex.fn.
  • Added missing asCallback and nodeify methods for QueryBuilder.

0.2.1 ​

What's new ​

  • bugfix: Chaining insert with returning now returns all listed columns.

0.2.0 ​

What's new ​

  • New name objection.js.
  • $beforeInsert, $afterInsert, $beforeUpdate and $afterUpdate hooks for Model.
  • Postgres jsonb query methods: whereJsonEquals, whereJsonSupersetOf, whereJsonSubsetOf and friends.
  • whereRef query method.
  • Expose knex.raw() through Model.raw().
  • Expose knex.client.formatter() through Model.formatter().
  • QueryBuilder can be used to make sub queries just like knex's QueryBuilder.
  • Possibility to use a custom QueryBuilder subclass by overriding Model.QueryBuilder.
  • Filter queries/objects for relations.
  • A pile of bug fixes.

Breaking changes ​

  • Project was renamed to objection.js. Migrate simply by replacing moron with objection.

0.1.0 ​

First release.

MIT Licensed