balderdashy/waterline-sequel

Querying a joined table doesn't work if the table name and model name differ

jaroslav-muller opened this issue · 3 comments

This query should work but it throws an error:
models.collections.action.find({'actionType': {name: '1'}})

Error is:

TypeError: Cannot read property 'attributes' of undefined
    at CriteriaProcessor.processSimple (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:431:45)
    at CriteriaProcessor.process (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:583:17)
    at CriteriaProcessor.and (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:264:8)
    at CriteriaProcessor.expand (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:163:12)
    at /home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:520:14
    at Array.forEach (native)
    at expandCriteria (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:517:17)
    at CriteriaProcessor.processObject (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:506:3)
    at CriteriaProcessor.process (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:583:17)
    at CriteriaProcessor.and (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:264:8)
    at CriteriaProcessor.expand (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:163:12)
    at /home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:104:12
    at Array.forEach (native)
    at CriteriaProcessor.read (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:103:22)
    at WhereBuilder.single (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/where.js:153:40)
    at simpleWhere (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/index.js:300:16)

The source of the error seems to be that at line currentSchema = this.schema[tableName].attributes waterline uses table name 'actionType' (model name) where it should use 'action_type' (table name).

If you use the same string for table name and model name such a query works OK.

Also a simpler queries, which are not joining the action_type table work OK:

models.collections.action.find({'actionType': '1'})
...
models.collections.actiontype.find({'name': '1'})

Following is a complete script that reproduces the behaviour:

'use strict';

var adapter = require('sails-postgresql');
var Waterline = require('waterline');

var config = {
  adapters: {
    'sails-postgresql': adapter
  },
  connections: {
    postgres: {
      adapter: 'sails-postgresql',
      host: 'localhost',
      user: 'frontend',
      password: '',
      database: 'frontend'
    }
  },
  defaults: {
    globals: {
      adapters: true,
      models: true
    },
    migrate: 'safe'
  }
};

var models = {};

var waterline = new Waterline();
waterline.loadCollection(Waterline.Collection.extend({
  identity: 'Action',
  tableName: 'action',
  connection: 'postgres',

  schema: true,

  attributes: {
    on: {
      type: "string",
      required: true
    },
    lastChecked: {
      type: "datetime",
      columnName: "last_checked",
      defaultsTo: null
    },
    period: {
      type: "integer"
    },
    actionType: {
      columnName: "action_type",
      model: "ActionType",
      required: true
    }
  }
}));

waterline.loadCollection(Waterline.Collection.extend({
  identity: 'ActionType',
  tableName: 'action_type',
  connection: 'postgres',

  schema: true,

  attributes: {
    name: {
      type: "string"
    },
    shortName: {
      type: "string",
      columnName: "short_name"
    }
  },
  validationMessages: {}
}));

waterline.initialize(config, function(error, models) {
  if (error) {
    throw error;
  }

  models.collections.action.find({
    'actionType': {
      name: '1'
    }
  })
    .populate('actionType')
    .exec(console.log);
});

@jaroslav-muller: Hello, I'm a repo bot-- nice to meet you!

It has been 30 days since there have been any updates or new comments on this page. If this issue has been resolved, feel free to disregard the rest of this message and simply close the issue if possible. On the other hand, if you are still waiting on a patch, please post a comment to keep the thread alive (with any new information you can provide).

If no further activity occurs on this thread within the next 3 days, the issue will automatically be closed.

Thanks so much for your help!

This is still reproducible and it definitely feels like a bug.
Please, don't close it unless you either fix it or convince me it's not a bug.

@jaroslav-muller We don't support deep querying like this. In this case you would need to switch the query around and search the actionType and populate the action.