Skip to content

Migration from objection 3.x to 4.0 ​

This section lists the breaking changes in objection 4.0.

objection is an ES module package ​

objection is now published as an ES module only. Import it like any other ES module:

js
import { Model, raw, ref } from 'objection';

objection has no default export. Replace import objection from 'objection' with a namespace import, which contains the same values:

js
import * as objection from 'objection';

CommonJS code can still load objection with require(), because node 20.19 and newer can require() ES modules that don't use top-level await:

js
const { Model, raw, ref } = require('objection');

require('objection') returns the module namespace object, with all the named exports.

The package now has an exports map, so only objection and objection/package.json can be imported. Deep imports of internal files like objection/lib/... no longer work.

Dropped support for node < 20.19 ​

Objection 4.0 needs at least node 20.19, the first version of node 20 that can require() ES modules without a flag. Node 22 needs at least 22.12.

Dropped support for knex < 3.3 ​

Objection 4.0 needs at least knex 3.3.

Dropped support for TypeScript < 5.8 ​

The typings of objection 4.0 need at least TypeScript 5.8, the first version that understands that node can require() ES modules. TypeScript 5.8, 6 and 7 are supported.

objection declares typescript as an optional peer dependency, so npm refuses to install objection next to an older TypeScript, and pnpm and yarn warn. As npm doesn't match prereleases against the range, TypeScript betas and nightlies need --legacy-peer-deps or an overrides entry. 3.x needs TypeScript 5.0 since 3.3 and 5.1 since 3.8, without declaring it #485.

Dropped support for MySQL < 8 ​

Objection 4.0 needs at least MySQL 8.0.17, for window functions, for derived tables that refer to the outer query, and for JSON_OVERLAPS() in whereJsonHasAny() and whereJsonHasAll() and the MEMBER OF operator of BelongsToManyRelation. MySQL 5.7 reached its end of life in October 2023. 3.x keeps supporting it.

Many-to-many patches with join table extras run in one query ​

A many-to-many patch() / update() with join table extras updates the related rows by their ids in one query, instead of in batches of 500 values as in 3.8.0 and 3.9.0. The ids are bound as one value where the database allows it:

  • SQL Server reads them as one JSON value with openjson, which needs SQL Server 2016 or newer and a database compatibility level of 130 or higher. SQL Server 2014 reached its end of life in July 2024.
  • Binary ids, e.g. binary(16) UUIDs, can't be represented in JSON and bind one value per row. On SQL Server, such a patch fails above about 2100 related rows, and on SQLite before 3.32 above 999.
  • Composite ids on PostgreSQL bind one value per column, which fails above 65535 values, about 32,000 related rows with two-column ids.

Run such patches on fewer related rows at once, e.g. by filtering them in several queries in a transaction. 3.x keeps the batches.

Database errors are objection's own classes ​

The database error classes (DBError, UniqueViolationError, NotNullViolationError, ForeignKeyViolationError, ConstraintViolationError, CheckViolationError and DataError) are no longer imported from the db-errors library. objection includes its own copy of it, and doesn't depend on the db-errors package anymore.

The classes have the same names and properties, but instanceof checks against classes imported directly from the db-errors package no longer match the errors objection throws. Import the classes from objection instead:

js
// Before:
const { UniqueViolationError } = require('db-errors');

// After:
import { UniqueViolationError } from 'objection';

Model files given as paths ​

modelClass can still be an absolute file path, or a module name resolved using modelPaths. objection loads the file synchronously with require(), so the file can be a CommonJS module or an ES module. An ES module needs to export the model class as its default export, or as its only exported model class.

ES module model files can't use top-level await, or import modules that do, because require() can't load those. Paths without a file extension are resolved like require() resolves them, which finds .js files, but not .mjs files. Give the full file name for those, or pass the model class itself, or a function that returns it, which also avoids the problem of circular imports.

skipUndefined() is removed ​

The deprecated skipUndefined() method is removed. undefined values passed to where() and the other SQL builder methods, and undefined ids, throw. Leave out the calls with undefined values instead:

js
// Before:
Person.query().skipUndefined().where('firstName', req.query.firstName);

// After:
const query = Person.query();

if (req.query.firstName !== undefined) {
  query.where('firstName', req.query.firstName);
}

onConflict() with insertGraph() and upsertGraph() throws ​

insertGraph(), upsertGraph() and their *AndFetch() variants throw an error when the query also uses onConflict(), ignore() or merge(). 3.x ignored these clauses and logged a warning. A graph is inserted using many insert queries, so there's no single query to apply the clause to. Insert the rows that may conflict using a separate insert() query and relate them in the graph, for example using #dbRef or the relate option.

Relation expressions that name a relation twice throw ​

Relation expressions that name a relation twice at the same level, like '[movies.reviews, movies.actors]' or '[movies, movies.reviews]', throw an error. 3.x logged a warning and only used the last one. This applies to withGraphFetched(), withGraphJoined(), allowGraph(), joinRelated() and all other methods that take relation expressions, and to the graph options of insertGraph() and upsertGraph(). Merge the relations instead, like 'movies.[reviews, actors]', or name the deepest relation only, like 'movies.reviews', which also loads movies. Graph options can use an array like ['movies', 'movies.reviews'] to match a relation together with relations below it.

Unknown graph options throw ​

insertGraph(), upsertGraph() and their *AndFetch() variants throw an error for unknown keys in their options, like a misspelled noInset instead of noInsert. 3.x ignored them and logged a warning, so the operation ran with the default behaviour. insertGraph() also throws for the options of upsertGraph() that have no effect on an insert, which 3.x ignored silently: noUpdate, noDelete, noUnrelate, insertMissing, update, unrelate, fetchStrategy and allowMarkers. It accepts relate, noRelate, noInsert and allowRefs. Fix or remove the reported keys.

insertGraph() throws a ValidationError of type InvalidGraph for new models (models without id) below models that it relates, naming their data path. 3.x ignored them and logged a warning, as insertGraph() never writes anything below related models. Insert them with a separate query, or use upsertGraph() with the relate option, which also upserts the relations of related models. Models with ids below related models, like fetched models with loaded relations, are still ignored.

upsertGraph() sets the foreign key to null before deleting a BelongsToOneRelation row ​

When upsertGraph() deletes the related row of a BelongsToOneRelation because of pet: null or #delete, it first sets the owner's foreign key to null, like unrelate(), whatever the owner's jsonSchema says. 3.9 only did this when the schema allowed null for the key, and otherwise deleted the row first and left the owner's reference to the database. Now an owner whose schema doesn't allow null, like { type: 'integer' }, fails with a ValidationError, and a NOT NULL column fails with a database error, where ON DELETE CASCADE deleted the owner and ON DELETE SET NULL cleared the reference. Allow null in the schema and the column, or delete the owner itself if it can't exist without the related row.

upsertGraph() keeps the current to-one model for ignored models ​

In a HasOneRelation or HasOneThroughRelation, upsertGraph() keeps the current model when the graph only holds a model that it ignores: a model marked with #unrelate or #delete that isn't the current one, or a replacement that noInsert or noRelate skips. The relation is left as if it wasn't in the graph, as for BelongsToOneRelation since 3.9. 3.x deleted or unrelated the current model. To remove the current model, mark it with its own id, like { pet: { id: 1, '#delete': true } }, or pass pet: null. To-many relations are unchanged: an array is the full list, so pets: [{ id: 2, '#delete': true }] still removes the current pets.

offset() in updates and deletes throws ​

update(), patch() and delete() throw an error for offset() when the query is executed. knex leaves the offset out of these statements, and on SQL Server the limit too, so they modified other rows than the offset asked for. 3.9 logged a warning. On MySQL, delete() also throws for orderBy() together with limit(), as knex leaves out the order but keeps the limit, so the limit deleted rows in any order. Fetch the ids of the rows to modify in a separate query and filter by them with whereIn(), or drop the offset if it was never applied. A many-to-many patch() / update() with join table extras on MySQL and SQL Server already selects the related rows first, so it honours the offset and doesn't throw.

JSON field expressions throw on SQL Server and Oracle ​

JSON field expressions like ref('column:path') and 'column:path' keys in patch(), ref('column').castJson() and the whereJson*() methods of objection throw an error on SQL Server and Oracle. 3.x generated the SQL of PostgreSQL, which these databases reject, and logged a warning. Use the JSON methods of knex instead, like whereJsonPath() and jsonExtract(). Other databases than PostgreSQL, MySQL and SQLite still get a warning.

On MySQL and SQLite, objection writes the SQL of the database for json field expressions and the whereJson*() methods, where 3.x generated the SQL of PostgreSQL, which they reject or, worse, run with other results, e.g. MySQL reads the #> operator as the start of a comment. SQLite has no json containment, so whereJsonSupersetOf() and whereJsonSubsetOf() throw an error there.

Casts, json values and json methods on MySQL and SQLite ​

The casts of ref() and val() use the types of MySQL there, as CAST() on MySQL only accepts a few types. castText(), castInt(), castBigInt() and castBool() were rejected by MySQL before and are CHAR and SIGNED now. These change their results:

  • castFloat() is DOUBLE instead of FLOAT, and castReal() is FLOAT instead of DOUBLE, like float and real on PostgreSQL. Use castTo('FLOAT') or castTo('DOUBLE') for the previous types.
  • castDecimal() is DECIMAL(65,30) instead of DECIMAL, which rounds to integers. Use castTo('DECIMAL(10,2)') or similar for a fixed precision.
  • Casts of json values in field expressions turn json null into NULL, like on PostgreSQL and SQLite, instead of 0 or 'null'.

On MySQL and SQLite, val() with an object or array and val().castJson() are json values, CAST(? AS JSON) on MySQL and json(?) on SQLite, so they compare as json. Before, they were json text, which MySQL compared as a string. Pass JSON.stringify(value) to compare with json text.

On SQLite, ref('column').castJson() throws for a whole column, as SQLite can't tell json columns from text columns. Cast the column first, e.g. ref('name').castText().castJson(), or refer to a json path.

Missing and array join properties throw ​

Fetching relations with withGraphFetched(), $fetchGraph(), fetchGraph() or $relatedQuery() throws an error in these cases, where 3.x logged a warning and returned no related models:

  • Owner models loaded from the database are missing a join property, e.g. because a partial select() left out the id. Select the column in the owner query. Owner models built in memory, e.g. with fromJson(), may not have their keys yet and still find no related models, now without a warning. This includes the foreign key of a BelongsToOneRelation, which 3.x didn't report. Models returned by insert() count as built in memory, also with returning().
  • Related models come back without their join property, e.g. because the relation mapping uses column names that knexSnakeCaseMappers() maps to other property names. Use the property names of the models in the relation mapping.
  • An owner join property holds an array of keys, e.g. a JSON array column. Use a BelongsToManyRelation instead, or a ManyToManyRelation through a join table. $relatedQuery() without withGraphFetched() or $fetchGraph() keeps working for them.

upsertGraph() relates moved rows before it deletes ​

upsertGraph() relates the rows that move to existing owners of a HasManyRelation or HasOneRelation first, before it deletes or unrelates the rows that the graph removes. 3.x deleted first, so a row that moved out of a deleted parent was deleted with it by ON DELETE CASCADE, and failed with a foreign key without ON DELETE action. The update hooks of these relates now run before the delete hooks of the same call. Rows that wait for the keys or references of models that the graph inserts, e.g. a name from a #ref{} to an inserted model, are related after the inserts, as in 3.x, so the relate throws a NotFoundError when ON DELETE CASCADE deleted the row with its old parent.

A row related from another owner can no longer take the unique key of a row that the graph removes in its new place, e.g. with unique(ownerId, name): the relate fails with a UniqueViolationError. Delete the conflicting row with a separate query in the same transaction first. Replacing such a row by a new one, or renaming a kept row to its key, still works, as do relates into a HasOneRelation with a unique foreign key.

A new row can't take the unique key of a row that moves to a new owner either: the move waits for the insert of the new owner, so the row still holds its key when the new row is inserted. 3.x cleared the moved row's foreign key first, which fails for NOT NULL keys. Insert the new owner with a separate call first, or make the unique constraint deferred and run upsertGraph() in a transaction.

Swapping the rows of two HasOneRelation slots with a unique foreign key, e.g. [{ id: 1, pet: { id: 2 } }, { id: 2, pet: { id: 1 } }], works with unrelate, as in 3.x: where the database reports the key column as nullable, one of the rows clears its key with a plain update first, without validation or hooks, and takes its new key with its own write. A hook that cancels that write leaves the key cleared, as the unrelate of 3.x did. Swaps of NOT NULL keys, or of keys in JSON columns, are written directly, which works where the key isn't unique.

3.x upserted the models below a related model with a nested upsertGraph() call for each owner and relation, after all other writes of the graph. 4.0 fetches the rows of the related models with their relations together with the rest of the graph, and writes them in the same steps, so their deletes, inserts and patches run with the others of their kind instead of last. The options keep matching the full relation paths.

A related model with relations whose row doesn't exist throws a NotFoundError saying that the model doesn't exist, before anything is written. 3.x wrote the rest of the graph first and then failed in the nested call, or with a foreign key error.

Rows that the graph moves to another owner, by relating them into a HasManyRelation or HasOneRelation, through their own BelongsToOneRelation or through a key that another place writes, have their relations read where they end up: the siblings of a pet that moves to another person are that person's pets, so siblings: [] there unrelates all of that person's other pets, and the pets of its old owner stay. Rows keep their related rows when a key changes with them instead, e.g. through a code prop or a #ref{}. All errors are thrown before anything is written, and a moved row is written once, so its update hooks run once, in the order of the other writes of their kind. 3.x upserted the models below moved rows in a pass of their own after the rest of the graph, against their rows after the move, and wrote a moved row twice if the models below it changed its keys.

insertGraph() and upsertGraph() reject rows placed in conflicting ways ​

A graph that gives one row two different values for the same column in any two of its places, also for values that places read through the rows they relate, or the slot of a HasOneRelation two different rows, throws a ValidationError of type InvalidGraph that names both data paths, before anything is written. A row is identified by its table and id, and a #ref or #dbRef model is a place of the row that it refers to. For example:

  • the row is placed under two owners through the same foreign key, e.g. a pet in the pets of two people, also through #ref;
  • the row is placed under an owner through a HasManyRelation or HasOneRelation, and its own BelongsToOneRelation names another model, e.g. { id: 1, pets: [{ id: 4, owner: { name: 'new' } }] } or a child with another parent;
  • one place gives the row a foreign key prop that another place sets through a relation, e.g. [{ id: 1, children: [{ id: 2 }] }, { id: 2, parentId: 5 }];
  • the row appears twice with different values for the same prop;
  • two rows read a value through the rows they relate, and the places give it different values, e.g. two pets that list each other as siblings while they have different owners;
  • two places give the same HasOneRelation of an owner two different rows, or one row and null, also when one of the rows gets the owner's key through a foreign key prop or its own BelongsToOneRelation, e.g. { id: 1, profile: null } next to { id: 6, person: { id: 1 } }. A HasOneRelation with a filter only counts the rows placed through it;
  • the row is marked with #delete in one place and kept in another, or marked with #unrelate, which clears its key, while another place puts it under an owner;
  • one place clears a key that another place places rows by, e.g. [{ id: 1, pets: [{ id: 10 }] }, { id: 3, favoritePet: { id: 12, siblings: [{ name: 's' }] } }] with unrelate, where person 1's pets unrelate pet 12 while its siblings are placed at its owner, or a pet whose siblings leave out the pet that lists it among its own;
  • a many-to-many pair is marked with #unrelate in one place and related or kept in another, e.g. { id: 1, movies: [{ id: 3, '#id': 'm', '#unrelate': true }, { '#ref': 'm' }] }, as the pair has one join row;
  • a model is related twice through a many-to-many relation with different join table extras, e.g. { id: 1, movies: [{ id: 2, role: 'director' }, { id: 2, role: 'producer' }] }. A pair of models has one join row in 4.0, where 3.x wrote a join row for each listing. To relate a pair more than once, model the join table with an id column of its own and relate its rows instead, e.g. as a credits relation from the person to a Credit model that belongs to the movie.

3.x wrote these graphs in an order that the graph doesn't define, and often deleted rows that the graph names, like the root. A graph that places a row the same way more than once stays valid, e.g. { id: 1, pets: [{ id: 4, owner: { id: 1 } }] }, and so does a row in places that write different columns, e.g. as a child and as a boss. A model related twice through the same many-to-many relation with the same extras, e.g. { title: 'T', genres: [{ '#id': 'f', name: 'Fantasy' }, { '#ref': 'f' }] } or the same { id: 2 } listed twice, gets one join row, where 3.x wrote one for each listing. A foreign key prop that a model gives next to its own relation, e.g. an old parentId next to a new parent, still follows the relation, and so does the key of a model that a relation places, e.g. a fetched pet with its old ownerId moved into the pets of another person. A row that one place of an owner lists and another place of the same owner leaves out is kept, as the graph keeps it in the first place.

upsertGraph() fetches the ids of the models that it relates, with relate or through #dbRef, and throws a NotFoundError before anything is written if one of them doesn't exist and the graph doesn't insert it elsewhere, e.g. with insertMissing:

js
await Person.query().upsertGraph({ id: 1, pets: [{ id: 1 }, { id: 99 }] }, { relate: true });
// NotFoundError: model (id=99) does not exist. If you want to insert it with an id, use the insertMissing option

3.x only checked the related models that the graph gave relations of their own. For the others, it patched no row for a HasManyRelation or HasOneRelation, and wrote the foreign key or the join row of the missing model for a BelongsToOneRelation or ManyToManyRelation. Code that relied on these keys to missing rows, e.g. without foreign key constraints, needs to insert the rows first, or use insertMissing. #ref models are the models they refer to, and are only fetched when they have relations.

insertGraph() and upsertGraph() errors name the index of array input ​

The data paths of models in array input start with their index in the array. This applies to the keys of the data of a ValidationError, the dataPath of a NotFoundError, and the dataPath option that a custom Validator gets. In 3.x, all models of the array had the same paths, so the path didn't tell which one failed:

js
await Person.query().insertGraph([{ firstName: 'Jennifer' }, { pets: [{ name: 1 }] }]);
// 3.x: err.data['pets[0].name']
// 4.0: err.data['[1].pets[0].name']

NotFoundErrors give dataPath: [1, 'pets', 0] instead of ['pets', 0], and validators get '[1].pets[0]' instead of '.pets[0]'. Graphs of a single object keep their paths.

#ref to new models that the graph doesn't write throws ​

A #ref to a new model, or a #ref{} to the id of a new model, that the graph doesn't write throws a ValidationError of type InvalidGraph before anything is written, e.g. for a model that noInsert skips, or below a model that noRelate ignores or that is marked with #delete or #unrelate. Point the reference to a model that the graph writes, or give the referenced model its id. Other #ref{} read the models as the graph gives them, also in branches that the graph doesn't write. In 3.x, a #ref into a branch that noRelate, #delete or #unrelate leave out failed with an error, sometimes after writing, and a #ref{} into such a branch wrote the #ref{} text itself, also for ids. A plain #ref to a model that noInsert skipped silently wrote null, and a #ref{} to its id wrote null, or failed after writing in a key that the relation sets too. See the guide for the rule.

insertGraph() and upsertGraph() keep only the referenced branches below noInsert models ​

Below a model that noInsert skips, nothing is written, except for the branches that a #ref or #ref{} of a written model points into, directly or through other kept branches. In 3.9.0, one such reference kept the whole branch of the skipped model, with all its other related models.

An existing row that the graph only places below a skipped model, in a branch that no reference keeps, isn't kept there anymore. If its owner elsewhere in the graph leaves it out, it's removed there, like any row that the graph doesn't keep:

js
// Person 1 has pet 2.
await Org.query().upsertGraph(
  {
    id: 1,
    people: [
      { id: 1, pets: [] },
      { id: 2, favoritePet: { '#ref': 'cat' } },
      // Skipped by `noInsert`, but `cat` is referenced:
      { name: 'new', pets: [{ id: 2 }, { '#id': 'cat', name: 'Cat' }] },
    ],
  },
  { relate: true, noInsert: ['people'], allowRefs: true },
);
// 3.9.0: pet 2 is kept, as the reference to `cat` kept the whole branch.
// 4.0: pet 2 is deleted, as only the branch of `cat` is kept.

To keep such a row, place it where the graph writes it, or use noDelete for its owner's relation.

upsertGraph() writes only what changes ​

upsertGraph() doesn't patch existing models that the graph gives no changes, e.g. a model with only its id and an #id, or a related model whose BelongsToOneRelation key stays the same, so their update hooks don't run. 3.x patched them and ran the hooks. The same holds for a model whose changes noUpdate drops, where 3.9.0 ran its $beforeUpdate hook once without writing. The join rows of existing many-to-many models are patched only with the join table extras that change, and with none under noUpdate, where 3.x wrote all extras that the graph gives with every patch of the related model.

The keys that upsertGraph() changes under noUpdate are written with patch() instead of update(), so $beforeUpdate() and $afterUpdate() get patch: true in their options for them, as for the patches of other models. They were already validated as patches #415.

A relate or update that finds no row to write throws a NotFoundError, e.g. for a row moved below a new row while ON DELETE CASCADE deletes it with its old parent, or a row deleted by a concurrent query. 3.x reported success and returned the row. Writes that a beforeUpdate hook resolves, e.g. with cancelQuery(), and patches that leave no column to write still succeed #214.

upsertGraph() gives a row the same keys in all its places ​

A row that the graph places in several places gets the same keys at all of them, in any order of the places: the siblings, children and join rows of each place follow the key that one place writes, or the owner that the row moves to, instead of the key the row had. A key that the graph gives a row but doesn't write, e.g. under noUpdate or at a BelongsToOneRelation #ref place, is ignored: the rows related through it get the row's real key, where 3.x gave them the unwritten one.

A row that lists itself in a relation on its own key, e.g. in its own siblings, gives that relation no key, so the listing no longer conflicts with the keys that the row's other places give. Where 3.x threw a ValidationError of type InvalidGraph, such a graph is written, or throws the NotFoundError that asks for the relate option when a row it moves isn't related. A row that such a relation leaves out of its own list, e.g. a pet whose siblings name only the other pets, is removed from it, as in 3.x, also where the graph relates or moves the row.

A key that the graph changes in an existing row, or gives a row that isn't loaded, is patched before the rows that the graph relates by that key, at the same place or another one, so a foreign key with ON UPDATE CASCADE on that column holds. 3.x wrote these rows with the old key and relied on the cascade. The patch and its $beforeUpdate run before the inserts and $beforeInsert of these rows, and before the relates and patches of the rows that read the key. A patch that also waits for a key or #ref{} value of these rows throws a ValidationError of type InvalidGraph before anything is written. The same goes for rows whose #ref{} copies a value other than the id that the graph changes in another existing row, e.g. personCode: '#ref{p.code}', as a foreign key may reference it, so such a relate is no longer moved before the removals. Copies that wait for each other are written in the order of the graph #428 #491.

insertGraph() and upsertGraph() write in fewer queries ​

Rows that the graph deletes are deleted with one query per relation instead of one per owner, so the static beforeDelete and afterDelete hooks run once with all owners as items. Join rows and unrelated rows are still removed per owner.

On SQLite, rows that the graph inserts are inserted in batches, where 3.x inserted one row per query, so the static beforeInsert and afterInsert hooks get several models at once.

These batches go through the query builder's insert() with an array of rows. A custom QueryBuilder that sends arrays from insert() to insertGraph(), the usual 3.x way to insert several rows on SQLite, now calls itself until the process runs out of memory, without an error:

js
class MyQueryBuilder extends QueryBuilder {
  insert(data) {
    // Remove this override: `insert()` inserts arrays on SQLite itself.
    if (Array.isArray(data) && data.length > 1) {
      return this.insertGraph(data);
    }
    return super.insert(data);
  }
}

Remove such overrides, as insert() inserts several rows on SQLite itself now.

Custom relations that extend Relation declare keySide ​

insertGraph() and upsertGraph() read where the keys of a relation are stored from its keySide getter: 'owner' for keys in the owner rows, like BelongsToOneRelation, 'related' for keys in the related rows, like HasManyRelation, and 'join' for keys in the rows of a join table, like ManyToManyRelation. Relation classes that extend the built-in relations inherit it. Classes that extend Relation directly need to declare it, or graph writes through them throw not implemented:

js
class MyRelation extends Relation {
  get keySide() {
    return 'related';
  }
}

typescript: The typings are ES module declarations ​

The typings no longer use export =. They declare the named exports of the ES module they describe. Apart from the default import, ES module and bundler projects need no changes.

TypeScript projects that compile to CommonJS can require() objection with "module": "nodenext". With "module": "node16", TypeScript refuses to require() objection from CommonJS files. On TypeScript < 7, projects that use "module": "commonjs" with the old "moduleResolution": "node" keep working. TypeScript 7 removed that resolution mode, so CommonJS projects need "module": "nodenext" there.

Static hooks get the result in the shape that the query returns ​

The result of the static after* hooks is what the query returns, instead of an array that wraps it:

  • afterFind gets the model of first(), findById() and queries for to-one relations like pet.$relatedQuery('owner'), or undefined if nothing is found. With page() and range(), it still gets the array of models.
  • afterInsert gets the inserted model when a single model is inserted, and runs after the fetch of insertAndFetch(), with the fetched values.
  • afterUpdate and afterDelete get the number of updated or deleted rows. With patchAndFetchById(), patchAndFetch() and their update* variants, afterUpdate runs after the fetch and gets the fetched model, or undefined if nothing was updated.
  • In before* hooks, result is undefined instead of [].

Code like result.map(...), result[0] or result.length fails for single models, numbers and undefined. Use asArray() to get an array in all cases:

js
import { Model, asArray } from 'objection';

class Person extends Model {
  static afterFind({ result }) {
    for (const person of asArray(result)) {
      // ...
    }
  }
}

The static after* hooks run as the last step of the query, after first(), page()'s models and the fetches of the *AndFetch() methods, but before the runAfter() callbacks, including the ones in context(), throwIfNotFound() and traverse(). These callbacks now get the result in the shape that the query returns and after the hooks, e.g. the model with first() also if runAfter() is called before first(), and they run before page() and range() wrap the result in { results, total }.

The outer queries of upsertGraph() and upsertGraphAndFetch() no longer call the static beforeFind and afterFind hooks of the root model class with the input models, nor $afterFind on the input models that they return. The queries that run for the models of the graph and their relations call the hooks as before. Likewise, fetchGraph() / $fetchGraph() and the relations that skipFetched skips no longer call the static beforeFind hook of the models they already have, as they didn't call afterFind: the find hooks are only called for queries that run.

TypeScript: StaticHookArguments<M, R> declares result: R, with R = undefined by default, and the static hooks of Model declare the shapes that they get, e.g. StaticHookArguments<any, Model | Model[] | undefined> for afterFind. Overrides that destructure result from StaticHookArguments<Person> need the type of the result as second type argument, e.g. StaticHookArguments<Person, Person | Person[] | undefined>.

withGraphFetched() assigns what the relation query returns ​

withGraphFetched() assigns what a relation query returns to the owners, after the static afterFind hook of the related model class and the runAfter() callbacks of the relation query, e.g. in modifiers or modifyGraph(). 3.x assigned the related models before both and ignored their return values. Related models that they filter out are no longer assigned, and the order that they return is kept. They can only return the related models that the query found. Other objects, e.g. copies like result.map((pet) => ({ ...pet })) or a count, throw an error. Change the models in place instead, or transform them in runAfter() of the root query.

withGraphJoined() now calls the static afterFind hooks of the joined related model classes too, and assigns what they return in the same way. The relations have no query of their own, so asFindQuery() and cancelQuery() throw an error in these hooks. afterFind hooks that call them fail for joined relations. beforeFind isn't called for joined relations.

limit, offset and first apply per parent in graph modifiers ​

The limitPerParent graph option of withGraphFetched defaults to true, so limit, offset and first in graph modifiers and in the modify of relation mappings apply to the related items of each parent instead of to the whole query that fetches the relation for all parents:

js
// Up to three pets per person. In 3.x, up to three pets for all persons together.
const people = await Person.query()
  .modifiers({
    latest: (query) => query.orderBy('createdAt', 'desc').limit(3)
  })
  .withGraphFetched('pets(latest)');

The relation query becomes a subquery, and its order moves into a row_number() window function. The order can only refer to the related table's columns that are selected and to the aliases of the result columns, see limiting related items per parent.

To keep the behaviour of 3.x, set limitPerParent: false in the graph options, or in defaultGraphOptions for all queries of a model.

typescript: insertGraph() has no allowMarkers option ​

InsertGraphOptions no longer has the deprecated allowMarkers option. insertGraph() rejects #unrelate and #delete markers since 3.8.0, so the option had no effect. Remove it from insertGraph() calls.

typescript: RelateQueryBuilder takes the number query builder ​

The type parameter of RelateQueryBuilder is the number query builder that relate() returns, instead of the query builder relate() is called on. Custom query builders that override relate() with an explicit return type use RelateQueryBuilder<NumberQueryBuilder<this>> instead of RelateQueryBuilder<this>.

typescript: Custom query builders declare RebindType ​

Custom query builders declare one RebindType member, from which the query builder types of first(), findById(), patch(), page() and the other methods that change the result type are derived. The five *QueryBuilderType members of 3.x are removed. Replace them:

ts
// 3.x
class MyQueryBuilder<M extends Model, R = M[]> extends QueryBuilder<M, R> {
  declare ArrayQueryBuilderType: MyQueryBuilder<M, M[]>;
  declare SingleQueryBuilderType: MyQueryBuilder<M, M>;
  declare MaybeSingleQueryBuilderType: MyQueryBuilder<M, M | undefined>;
  declare NumberQueryBuilderType: MyQueryBuilder<M, number>;
  declare PageQueryBuilderType: MyQueryBuilder<M, Page<M>>;
}

// 4.0
class MyQueryBuilder<M extends Model, R = M[], X = unknown> extends QueryBuilder<M, R, X> {
  declare RebindType: MyQueryBuilder<this['~M'], this['~R'], this['~X']>;
}

The third type parameter X holds extra types of the query, which strict mode uses to check the names of modifiers, see Checking queries in TypeScript. Custom query builders pass it on to QueryBuilder and RebindType. Models whose query builder doesn't are type errors on the model class, about the types returned by methods like castTo(), which don't mention X.

The helper types SingleQueryBuilder, MaybeSingleQueryBuilder, ArrayQueryBuilder, NumberQueryBuilder and PageQueryBuilder take any query builder, e.g. MaybeSingleQueryBuilder<this> as the return type of an overridden findById(), and NumberQueryBuilder<MaybeSingleQueryBuilder<this>> for patchById(), updateById() and deleteById(), the writes of findById(). WithGraphModelQueryBuilder<M, E> is removed. Use QueryBuilderType<WithGraphModel<M, E>> instead. The ReturningMethod interface is removed too, returning() is a method of QueryBuilder. On query builders with an any result, e.g. AnyQueryBuilder, it returns QueryBuilder<any, any> | QueryBuilder<any, any[]> instead of QueryBuilder<any, any[]>.

typescript: Internal helper types are no longer exported ​

The helper types of the relation expression parser, the withGraph*() narrowing and the property names, like ParseRelationString, WithGraph, WithGraphProperty, InsertablePropertyNames and RelationPropertyNames, are no longer exported, and the unused ItemType is removed. Use WithGraphModel, WithGraphQueryBuilder, ModelProps and ModelRelations instead.

typescript: Model.jsonSchema is a readonly schema ​

static jsonSchema is typed ReadonlyJSONSchema, a JSON schema with readonly arrays, so that schemas declared as const can be assigned to it, for FromJSONSchema. A JSONSchema is assignable to it, but not the other way around: a model's schema read through the static type can't be passed to a parameter typed JSONSchema, or returned from $beforeValidate(). Declare the type on models that are used that way:

ts
import { JSONSchema, Model } from 'objection';

class Person extends Model {
  static jsonSchema: JSONSchema = {
    type: 'object',
    required: ['firstName'],
    properties: { firstName: { type: 'string' } }
  };
}

function describeSchema(schema: JSONSchema) {
  // ...
}

describeSchema(Person.jsonSchema);

Code that reads the schema of any model class, e.g. modelClass.jsonSchema in plugins or helpers, takes it as ReadonlyJSONSchema.

typescript: insert(), patch() and update() reject relation data ​

insert(), patch(), update() and their variants, merge() and relate() only write the columns of the model's own table and ignore relation properties. Their argument type is PartialModelRow<M>: a model instance, or the model's properties without relations. Objects with relation data are type errors, and so are values whose type includes the relation properties, even if they hold no relation data: Partial<Person>, ModelObject<Person> (e.g. the result of toJSON()) and PartialModelObject<Person>, which these methods took in 3.x. Type such values as PartialModelRow<Person>, or pass the model instance:

ts
// 3.x: accepted, and `pets` silently ignored
await Person.query().insert({ firstName: 'Jennifer', pets: [{ name: 'Fluffy' }] });

// 4.0: insertGraph() inserts the relations
await Person.query().insertGraph({ firstName: 'Jennifer', pets: [{ name: 'Fluffy' }] });

// 4.0: type data without relations as PartialModelRow
function insertPerson(data: PartialModelRow<Person>) {
  return Person.query().insert(data);
}

Custom query builders that override these methods use PartialModelRow<M> for the data instead of PartialModelObject<M>, and PartialModelPatch<M> for patch(), its variants and merge(), which also takes field expression keys like 'address:street'.

typescript: Property types leave out $ properties ​

ModelObject, PartialModelObject and the other property types leave out properties starting with $, which objection treats as internal, like toJSON() does.

typescript: $relatedQuery() of to-one relations may resolve to undefined ​

$relatedQuery() of a BelongsToOneRelation, HasOneRelation or HasOneThroughRelation resolves to undefined when there is no related row, and its type says so now:

ts
const parent = await person.$relatedQuery('parent'); // Person | undefined

// Throw instead, if the row has to exist:
const parent = await person.$relatedQuery('parent').throwIfNotFound(); // Person

Writes of to-one relation queries with returning() resolve to one model ​

patch(), update() and delete() with returning through $relatedQuery() of a BelongsToOneRelation, HasOneRelation or HasOneThroughRelation resolve to the written model, or undefined if there is no related row, like the find query does: only several written rows, which a has-one relation can have, stay an array. 3.x resolved to an array. The result of the static afterUpdate() and afterDelete() hooks of these writes is the model too. relatedQuery() with for() still resolves to an array:

js
// Before:
const [parent] = await person.$relatedQuery('parent').patch({ age: 50 }).returning('*');

// After:
const parent = await person.$relatedQuery('parent').patch({ age: 50 }).returning('*');

The typings give writes with returning() the result kind of their query everywhere: one model, or undefined, after $query(), findById(), first(), the *ById() methods and $relatedQuery() of to-one relations, also with returning() before the write, and an array otherwise. They typed writes of findById(), first() and the *ById() methods as arrays, although they resolve to one model too. Custom query builders that override patchById(), updateById() or deleteById() declare NumberQueryBuilder<MaybeSingleQueryBuilder<this>> as their return type, see Custom query builders declare RebindType.

typescript: toJSON() returns ModelJson ​

toJSON() and $toJson() return ModelJson<M>, which describes the plain object they produce: the model's properties without methods, and the related models as ModelJson too. They returned ModelObject<M>, whose relations claimed model instances. Type the values that hold the result of toJSON() as ModelJson<M> instead of ModelObject<M>:

ts
function send(person: ModelJson<Person>) {
  // ...
}

send(person.toJSON());

typescript: relate().returning() rows are plain objects ​

The rows of relate().returning() are typed Pojo[] instead of any[]: depending on the relation, they are the join rows, the related rows or the owner rows, but not models of the query. Cast them to the rows you expect:

ts
const rows = await person.$relatedQuery('movies').relate(50).returning('*');
const joinRows = rows as { personId: number; movieId: number }[];

insert and update reject field expression keys ​

insert and update write whole values, so they throw a ValidationError of type ModelValidation for field expression keys like 'meta:color', which write parts of json columns. 3.x update wrote them with jsonb_set, and 3.x insert passed them to the database as column names. Use patch for them:

js
// Before:
await Person.query().update({ name: 'Jennifer', 'meta:color': 'red' }).where('id', 1);

// After:
await Person.query().patch({ name: 'Jennifer', 'meta:color': 'red' }).where('id', 1);

Keys with a : that the model's jsonSchema declares in its properties, like 'ns:name', are normal properties and columns, and insert and update write them as before. To insert into a column with a : in its name, declare it in the schema.

snakeCaseMappers keeps the JSON keys of field expressions ​

snakeCaseMappers only converts the column part of field expressions used as property names, and keeps their JSON keys as written: the preserveJsonKeys option defaults to true. 3.x converted the JSON keys too:

js
// 4.0: update "persons" set "json_column" = jsonb_set(COALESCE("json_column", '{}'), '{someKey}', '1', true)
// 3.x: update "persons" set "json_column" = jsonb_set("json_column", '{some_key}', '1', true)
await Person.query().patch({ 'jsonColumn:someKey': 1 });

If your JSON columns contain snake_case keys written this way, set preserveJsonKeys: false to keep converting them:

js
class Person extends Model {
  static get columnNameMappers() {
    return snakeCaseMappers({ preserveJsonKeys: false });
  }
}

Keys with a : that the model's jsonSchema declares in its properties are columns, not field expressions, and are converted as a whole. A column like ns:foo_bar is read as ns:fooBar and only written back to ns:foo_bar if the schema declares ns:fooBar. Declare such columns in the schema. knexSnakeCaseMappers never converted the JSON keys and is unchanged.

Aggregates don't select the columns they're passed ​

Aggregates like count() and sum() select their aliases, not the columns they're passed, but they still replace the select all of a query. When a query with aggregates loads relations with withGraphFetched() or withGraphJoined(), objection selects the columns it needs to assign the related models. 3.x treated count('* as n') as selecting all columns and count('ownerId') as selecting ownerId, so these columns were missing and the relations came back empty:

js
// 4.0: select count(*) as "n", "pets"."ownerId" from "pets" group by "ownerId"
// 3.x: select count(*) as "n" from "pets" group by "ownerId", and every owner is null
await Pet.query().count('* as n').groupBy('ownerId').withGraphFetched('owner');

Grouped queries need to group by the added columns too, or the database rejects them. Add the join columns to groupBy(), like groupBy('ownerId') above. In withGraphJoined(), the root query selects only its id columns next to the aggregates instead of all its columns. Select the columns you need explicitly.

Misplaced * and ^ in relation expressions throw ​

* and ^ end a relation path. 3.x silently ignored everything after them, so 'parent.^.pets' fetched parent recursively without pets, and '*.pets' fetched all relations. It also ignored modifiers and aliases on them, like 'parent.^(onlyAdults)', and treated * and ^ in a list, like '[children, *]', as relation names. These expressions throw an error in 4.0, in all methods that take relation expressions and in the graph options of insertGraph() and upsertGraph(). Remove what follows * or ^, and apply modifiers to the relation itself, like 'parent(onlyAdults).^'.

$setRelated and $appendRelated sync foreign keys ​

$setRelated and $appendRelated set the foreign keys to match the relation. 3.x only assigned the related models:

js
post.$setRelated('author', user); // post.authorId === user.id
post.$setRelated('author', null); // post.authorId === null

user.$setRelated('posts', [post1, post2]); // post1.authorId === post2.authorId === user.id
user.$setRelated('posts', [post2]); // post1.authorId === null, unless it already pointed to another user
new User().$setRelated('posts', [post3]); // post3.authorId === null, as the user has no id yet

The synced keys show up in toJSON() and are written by a later patch(), update() or upsertGraph() of these models. Code that assigned related models only to serialize them, and relied on their foreign keys staying as they were, can assign the relation property directly, e.g. post.author = user.

findById() and findByIds() keep their id condition in its own group ​

findById and findByIds put their id condition in its own parentheses, like $query() and patchById(), updateById() and deleteById() already do. 3.x added it to the other where clauses of the query, so an orWhere() widened it:

js
// 4.0: where "persons"."id" = 1 and ("slug" = 'jennifer')
// 3.x: where "persons"."id" = 1 or "slug" = 'jennifer'
await Person.query().findById(1).orWhere('slug', 'jennifer');

Queries that relied on this, like a lookup by id or slug, need to spell out both conditions:

js
await Person.query().where('id', idOrSlug).orWhere('slug', idOrSlug).first();

JSON attributes are parsed by column format ​

The PostgreSQL drivers return json and jsonb columns parsed. 3.x parsed every string that came out of them a second time, so a JSON attribute that holds the string '123' came back as the number 123, and saving the model wrote the change back. 4.0 keeps the values that these drivers return, so JSON stored in text columns comes back as a string. Change these columns to jsonb. On PostgreSQL 16 or newer, this also keeps the rows that hold plain text, as JSON strings:

sql
ALTER TABLE persons ALTER COLUMN address TYPE jsonb
  USING CASE WHEN address IS JSON THEN address::jsonb ELSE to_jsonb(address) END;

This turns '{"a":1}', 'plain text', '123' and NULL into {"a":1}, "plain text", 123 and null, deciding once what each row holds. Where the column can't be changed, tell objection what kind of column it is: 'text' for JSON text, which it parses strictly, throwing for invalid JSON, and 'mixed' for JSON mixed with plain text, where it parses the strings that are valid JSON and keeps the others, as 3.x did:

js
class Person extends Model {
  static jsonColumnFormat = 'text';
}

The format applies to all JSON attributes of a model, not per column. With 'mixed', a string that looks like JSON, e.g. '123', comes back as the number 123, also from the model's json and jsonb columns.

On SQLite and SQL Server, which return JSON as text, JSON attributes are parsed strictly, so a string that isn't valid JSON throws Invalid JSON in JSON attribute …, where 3.x kept the string. Set jsonColumnFormat = 'mixed' on models whose JSON columns also hold plain text there. MySQL drivers parse JSON attributes as in 3.x, so with mysql2, which returns json columns parsed, a string in them that looks like JSON, e.g. '123', comes back as the number 123. Set jsonColumnFormat = 'json' with mysql2 on MySQL to keep such strings, but not with mysql, which returns JSON as text, and not on MariaDB, which stores it as text. On MariaDB, which stores JSON as LONGTEXT, jsonColumnFormat = 'text' avoids guessing whether a string is JSON.

Overrides of the static fromDatabaseJson, $setDatabaseJson and $parseDatabaseJson get the query's options as second argument. Pass them on to super, or JSON attributes are parsed as in 3.x.



Migration from objection 2.x to 3.0 ​

This document guides you through each breaking change in objection 3.0 and attempts to provide clear steps to follow. If you find something missing, please open an issue and we'll fix this guide ASAP.

Here's a list of the breaking changes

Dropped support for node < 12 ​

Objection 3.0 needs at least node 12 to work.

Dropped support for knex < 0.95 ​

Objection needs at least knex 0.95.0 to work.

This is because knex introduced some breaking changes in 0.95.0.

typescript: Methods like findById and findOne that return a single item are now typed correctly ​

In 2.0 methods like findById, findOne and first that return one item or undefined were incorrectly typed to always return a value. Now the return type of those methods is SomeModel | undefined.

You'll get compilation errors in places where you've trusted the old typings. All you need to do is to add a check for undefined values to narrow down the type or chain the throwIfNotFound method that also narrows down the type back to SomeModel.

Before:

ts
const person = await Person.query().findById(id)
console.log(person.id)

After:

ts
const person = await Person.query().findById(id)

if (!person) {
  throw new Error('Person not found')
}

console.log(person.id)

Or

ts
const person = await Person.query().findById(id).throwIfNotFound()
console.log(person.id)

typescript: QueryBuilder now inherits PromiseLike instead of Promise ​

In 2.0 and before, the QueryBuilder class inherited Promise in the typings which made code like this possible

ts
function findPerson(id: number): Promise<Person> {
  return Person.query().findById(id);
}

However, QueryBuilder doesn't actually inherit Promise but instead is "thenable". Typescript has the type PromiseLike for thenable objects which objection now correctly uses. There are couple of ways to fix the compilation errors this change causes:

  1. Chain execute to the query:
ts
function findPerson(id: number): Promise<Person> {
  return Person.query().findById(id).execute();
}
  1. Use PromiseLike:
ts
function findPerson(id: number): PromiseLike<Person> {
  return Person.query().findById(id);
}
  1. Make the functions async:
ts
async function findPerson(id: number): Promise<Person> {
  return Person.query().findById(id);
}

Dropped a bunch of deprecated methods and features ​

All the methods that were marked deprecated (and printed a deprection message during runtime) in 2.0 have now been removed.

















Migration from objection 1.x to 2.0 ​

Objection 2.0 brought a lot of great new features, but the main focus was in cleaning the API, which lead to a bunch of breaking changes. This document guides you through each change and attempts to provide clear steps to follow. If you find something missing, please open an issue and we'll fix this guide ASAP.

Here's a list of the breaking changes

In addition to these, a lot of methods were deprecated and replaced by a new method. The old methods still work, but they print a warning (once per process) when you use them. The warning message tells which method you should be using in the future and you can slowly replace the methods as you get annoyed by the warnings.

Most of the methods have simply been renamed, but in some cases the replacing methods work a little differently. Make sure to read the documentation of the new method.

Node 6 and 7 are no longer supported ​

Objection now needs at least node 8 to work.

modify method signature change ​

Before, you were able to provide multiple modifiers or modifier names to modify by providing multiple arguments like this:

js
Person.query().modify('foo', 'bar');

Now only the first argument is used to specify modifiers and all the rest are arguments for the modifiers. The first argument can be an array so you can simply wrap the modifiers in an array if there are more than one of them:

js
Person.query().modify(['foo', 'bar']);

Bluebird and lodash have been removed ​

Before, all async operations returned a bluebird promise. Now the bluebird dependency has been dropped and the native Promise is used instead. This also means that all bluebird-specific methods

  • map
  • reduce
  • reflect
  • bind
  • spread
  • asCallback
  • nodeify

have been removed from the QueryBuilder.

You need to go through your code and make sure you don't use any bluebird methods or trust that objection returns a bluebird promise.

Objection also used to export Promise and lodash properties like this:

js
import { Promise, lodash } from 'objection';

which is not true anymore. Both of those exports have been removed.

Database errors now come from the db-errors library ​

Before, when a database operation failed, objection simply passed through the native error thrown by the database client. In 2.0 the errors are wrapped using the db-errors library.

The db-errors library errors expose a nativeError property. If you rely on the properties of the old errors, you can simply change code like this

js
try {
  await Person.query().where('foo', 'bar')
} catch (err) {
  if (err.code === 13514) {
    ...
  }
}

into this:

js
try {
  await Person.query().where('foo', 'bar')
} catch (err) {
  err = err.nativeError || err

  if (err.code === 13514) {
    ...
  }
}

A preferred way to handle this would be to use the new db-error classes as described here, but the fastest migration path is to do the above trick.

#ref references in insertGraph and upsertGraph now require the allowRefs: true option ​

The usage of '#ref': 'someId' and #ref{someId.someProp} now requires an explicit allowRefs: true option to be passed to the called method:

js
await Person.query().insertGraph(graphWithRefs, { allowRefs: true });

This change was made for security reasons. An attacker could, in theory, use a #ref{someId.someProperty} reference to access for example the password hash of a user:

js
const graphUpsertSentByTheAttacker = {
  user: {
    id: 13431,
    '#id': 'user',
  },

  movie: {
    name: '#ref{user.passwordHash}',
  },
};

and then the attacker could just take the password hash out of the movies's name in a client of some sort.

For this attack to work, the attacker must already have an access to the API that modifies the user's information. Additionally and more importantly, the graph described above (and all other graphs I could think of) would only yield the password hash in the movie's name if the program sets the hash to the graph before the upsertGraph call is executed. This is a highly unlikely scenario and in case of passowords, would require the attacker to be able to access a route that changes the user's password. The reference can never access the property in the database, only in the object itself.

Even though there's very little chance this kind of attack could be carried out at the moment, I'd advice you to never use upsertGraph with { allowRefs: true } and unvalidated user input with references!

relate method now always returns the number of affected rows ​

relate used to return the inserted pivot table row in case of ManyToManyRelation and the number of updated rows in case of other relations. Now an integer indicating the number of affected rows is always returned.

You need to go through your code and check how the return values of relate queries are used.

$relatedQuery no longer mutates ​

With objection 1.x, doing this

js
await somePerson.$relatedQuery('pets');

added a property pets for somePerson and saved the result there. This no longer happens with objection 2. Also inserting a new item using

js
await somePerson.$relatedQuery('pets').insert(pet);

would previously add the inserted pet to the pets array of somePerson. This also no longer happens.

You can use withGraphFetched and fetchGraph methods if you want to populate the relations. Also, nothing prevents you from doing this:

js
somePerson.pets = await somePerson.$relatedQuery('pets');

You can also use the Model.relatedFindQueryMutates and Model.relatedInsertQueryMutates properties to revert back to 1.x behavior.

context() now acts like mergeContext() ​

The context method of QueryBuilder now merges the given object with the current context object instead of replacing it. You can use clearContext to clear the context if you need the old behaviour.

Model.raw and Model.fn now return objection raw and fn ​

Previously Model.raw returned a knex raw builder and Model.fn returned a knex FunctionHelper instance. In 2.0 objection's raw and fn helpers are returned. To get a knex raw builder, you need to use knex.raw directly.

QueryBuilder.toString and QueryBuilder.toSql have been removed ​

You can use QueryBuilder.toKnexQuery().toSQL() instead.

Rewritten typings ​

The typings have been completely rewritten in 2.0 and many of the types have changed names. By default you shouldn't get that many errors, but whenever you have explicitly defined an objection type for something other than Model, you may need to adjust that type. For example the QueryBuilder no longer takes three generic arguments, but two.

For this breaking change we can't easily provide clear migration steps, because we don't know how much you have trusted the typescript type inference, and how much you have used explicit types.

One notable change is that you should no longer define your relations properties using Partial<Model> or Partial<Model>[]. You can simply use Model and Model[] and methods like insertGraph and upsertGraph will just work.

MIT Licensed