Skip to content

Commit

Permalink
only supply the id attribute to knex.insert for drivers that support it
Browse files Browse the repository at this point in the history
Knex 0.16.4 began warning when the returning attribute is supplied for
dialects that do not support it.  These dialects currently include MySQL
and SQLite3.  Supplying this attribute is a no-op for those dialects, so
excluding them is safe and eliminates the warning.

More information:

    knex/knex#3039
  • Loading branch information
Mike Eldridge committed Jan 15, 2020
1 parent 7a40af5 commit ae1622c
Showing 1 changed file with 8 additions and 1 deletion.
9 changes: 8 additions & 1 deletion src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -421,6 +421,13 @@ function loadWithRelations (items, resourceConfig, options) {
Adapter.extend({
constructor: SqlAdapter,

_returning (idAttribute) {
// Some knex client drivers do not support .returning(), so set the
// attribute to null to prevent knex from emitting warnings.

return ['mysql', 'sqlite3'].includes(this.knexOpts.client) ? null : idAttribute
},

_count (mapper, query, opts) {
opts || (opts = {})
query || (query = {})
Expand All @@ -438,7 +445,7 @@ Adapter.extend({

const sqlBuilder = utils.isUndefined(opts.transaction) ? this.knex : opts.transaction
return sqlBuilder(getTable(mapper))
.insert(props, idAttribute)
.insert(props, this._returning(idAttribute))
.then((ids) => {
const id = utils.isUndefined(props[idAttribute]) ? (ids.length ? ids[0] : undefined) : props[idAttribute]
if (utils.isUndefined(id)) {
Expand Down

0 comments on commit ae1622c

Please sign in to comment.