Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
jaroslav-muller opened this issue Mar 4, 2016 · 3 comments

Comments

@jaroslav-muller
Copy link

@jaroslav-muller jaroslav-muller commented Mar 4, 2016

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);
});
@sailsbot

This comment has been minimized.

Copy link
Collaborator

@sailsbot sailsbot commented Apr 4, 2016

@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!

@jaroslav-muller

This comment has been minimized.

Copy link
Author

@jaroslav-muller jaroslav-muller commented Apr 5, 2016

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.

@particlebanana

This comment has been minimized.

Copy link
Contributor

@particlebanana particlebanana commented Apr 11, 2016

@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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
3 participants
You can’t perform that action at this time.