Skip to content

Better error for unsupported lock modes #6337

@mercmobily

Description

@mercmobily

Calling lock methods like .forNoKeyUpdate() or .forKeyShare() on dialects that don't support them throws a confusing TypeError. It would be clearer to throw a friendly “not supported by this dialect” error instead.

The builder sets single.lock when you call a lock method (forUpdate, forShare, forNoKeyUpdate, forKeyShare). The query compiler then does:

  if (this.single.lock) {
    return this[this.single.lock]();
  }

If the dialect compiler doesn’t implement that method, it throws: TypeError: this[this.single.lock] is not a function

The error is confusing and looks like a Knex bug, not a clear unsupported‑feature message. It doesn’t tell the user which lock mode is unsupported. Also, the error displays in the new web site when it tries to run the query for unsupported dilalects (which is how I found this).

In lib/query/querycompiler.js, add a guard so it throws a clearer error:

  if (this.single.lock) {
    if (typeof this[this.single.lock] === 'function') {
      return this[this.single.lock]();
    }
    throw new Error('Locking method not supported by this dialect');
  }

Optionally, include the lock name in the message: Locking method 'forNoKeyUpdate' not supported by this dialect

This keeps behaviour the same for supported dialects and makes unsupported cases understandable.

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions