Bookshelf: Undefined binding(s) detected when compiling

Created on 25 Aug 2016  路  3Comments  路  Source: bookshelf/bookshelf

I've looked through all the issues raised here, and this does not seem to be covered, nor should it be happening, but it is to me.

My code

let User = bookshelf.Model.extend({
  tableName: 'users',
  hasTimestamps: true,
  type: function() { return this.belongsTo(UserType) }
}, {
  findAll: Promise.method(args =>
    User
      .forge()
      .fetchAll({ withRelated: 'type' })
      .then(users => users.toJSON()))
)

Without the { withRelated: 'type' } the code runs fine and all users are returned

"users": [
      {
        "id": "16",
        "email": "[email protected]",
        "access_token": "quite long",
        "confirmation_token": ""
      },
      {
        "id": "22",
        "email": "[email protected]",
        "access_token": null,
        "confirmation_token": ""
      }
]

but when I try to get the related type

"errors": [
    {
      "message": "Undefined binding(s) detected when compiling SELECT query: select `user_types`.* from `user_types` where `user_types`.`id` in (?)",
      "locations": [
        {
          "line": 38,
          "column": 2
        }
      ],
      "stack": "Error: Undefined binding(s) detected when compiling SELECT query: select `user_types`.* from `user_types` where `user_types`.`id` in (?)\n    at QueryCompiler_MySQL.toSQL..."

The debug output

{ method: 'select',
  options: {},
  timeout: false,
  cancelOnTimeout: false,
  bindings: [],
  __knexQueryUid: 'a733c03d-86c8-4cb7-8830-49ee6f629165',
  sql: 'select `users`.* from `users`' }

What could be the issue. Been on this for hours now.

question

Most helpful comment

Hi.
Actually that was the error. Fixed with

let User = bookshelf.Model.extend({
  tableName: 'users',
  hasTimestamps: true,
  type: function() { return this.belongsTo(UserType, 'type_id') }, // Added the type_id
}, {
  findAll: Promise.method(args =>
    User
      .forge()
      .fetchAll({ withRelated: 'type' })
      .then(users => users.toJSON()))
)

Thanks.

All 3 comments

How does look your UserType model?

Hmm... Actually I don't see `user_type_id' field in your example result.

Hi.
Actually that was the error. Fixed with

let User = bookshelf.Model.extend({
  tableName: 'users',
  hasTimestamps: true,
  type: function() { return this.belongsTo(UserType, 'type_id') }, // Added the type_id
}, {
  findAll: Promise.method(args =>
    User
      .forge()
      .fetchAll({ withRelated: 'type' })
      .then(users => users.toJSON()))
)

Thanks.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

ADrejta picture ADrejta  路  3Comments

MarkHerhold picture MarkHerhold  路  3Comments

XavierGeerinck picture XavierGeerinck  路  3Comments

cuteboi picture cuteboi  路  4Comments

jhnferraris picture jhnferraris  路  4Comments