{"action":"created","issue":{"url":"https://api.github.com/repos/balderdashy/waterline-sequel/issues/82","repository_url":"https://api.github.com/repos/balderdashy/waterline-sequel","labels_url":"https://api.github.com/repos/balderdashy/waterline-sequel/issues/82/labels{/name}","comments_url":"https://api.github.com/repos/balderdashy/waterline-sequel/issues/82/comments","events_url":"https://api.github.com/repos/balderdashy/waterline-sequel/issues/82/events","html_url":"https://github.com/balderdashy/waterline-sequel/issues/82","id":138431874,"number":82,"title":"Querying a joined table doesn't work if the table name and model name differ","user":{"login":"jaroslav-muller","id":11649028,"avatar_url":"https://avatars.githubusercontent.com/u/11649028?v=3","gravatar_id":"","url":"https://api.github.com/users/jaroslav-muller","html_url":"https://github.com/jaroslav-muller","followers_url":"https://api.github.com/users/jaroslav-muller/followers","following_url":"https://api.github.com/users/jaroslav-muller/following{/other_user}","gists_url":"https://api.github.com/users/jaroslav-muller/gists{/gist_id}","starred_url":"https://api.github.com/users/jaroslav-muller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jaroslav-muller/subscriptions","organizations_url":"https://api.github.com/users/jaroslav-muller/orgs","repos_url":"https://api.github.com/users/jaroslav-muller/repos","events_url":"https://api.github.com/users/jaroslav-muller/events{/privacy}","received_events_url":"https://api.github.com/users/jaroslav-muller/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/balderdashy/waterline-sequel/labels/Waiting%20to%20close","name":"Waiting to close","color":"ededed"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":1,"created_at":"2016-03-04T09:53:21Z","updated_at":"2016-04-05T08:34:43Z","closed_at":null,"body":"This query should work but it throws an error:\r\n`models.collections.action.find({'actionType': {name: '1'}})`\r\n\r\nError is:\r\n```\r\nTypeError: Cannot read property 'attributes' of undefined\r\n    at CriteriaProcessor.processSimple (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:431:45)\r\n    at CriteriaProcessor.process (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:583:17)\r\n    at CriteriaProcessor.and (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:264:8)\r\n    at CriteriaProcessor.expand (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:163:12)\r\n    at /home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:520:14\r\n    at Array.forEach (native)\r\n    at expandCriteria (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:517:17)\r\n    at CriteriaProcessor.processObject (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:506:3)\r\n    at CriteriaProcessor.process (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:583:17)\r\n    at CriteriaProcessor.and (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:264:8)\r\n    at CriteriaProcessor.expand (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:163:12)\r\n    at /home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:104:12\r\n    at Array.forEach (native)\r\n    at CriteriaProcessor.read (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/lib/criteriaProcessor.js:103:22)\r\n    at WhereBuilder.single (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/where.js:153:40)\r\n    at simpleWhere (/home/jarin/tmp/test/node_modules/sails-postgresql/node_modules/waterline-sequel/sequel/index.js:300:16)\r\n```\r\nThe 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).\r\n\r\nIf you use the same string for table name and model name such a query works OK.\r\n\r\nAlso a simpler queries, which are not joining the action_type table work OK:\r\n```\r\nmodels.collections.action.find({'actionType': '1'})\r\n...\r\nmodels.collections.actiontype.find({'name': '1'})\r\n```\r\n\r\nFollowing is a complete script that reproduces the behaviour:\r\n```\r\n'use strict';\r\n\r\nvar adapter = require('sails-postgresql');\r\nvar Waterline = require('waterline');\r\n\r\nvar config = {\r\n  adapters: {\r\n    'sails-postgresql': adapter\r\n  },\r\n  connections: {\r\n    postgres: {\r\n      adapter: 'sails-postgresql',\r\n      host: 'localhost',\r\n      user: 'frontend',\r\n      password: '',\r\n      database: 'frontend'\r\n    }\r\n  },\r\n  defaults: {\r\n    globals: {\r\n      adapters: true,\r\n      models: true\r\n    },\r\n    migrate: 'safe'\r\n  }\r\n};\r\n\r\nvar models = {};\r\n\r\nvar waterline = new Waterline();\r\nwaterline.loadCollection(Waterline.Collection.extend({\r\n  identity: 'Action',\r\n  tableName: 'action',\r\n  connection: 'postgres',\r\n\r\n  schema: true,\r\n\r\n  attributes: {\r\n    on: {\r\n      type: \"string\",\r\n      required: true\r\n    },\r\n    lastChecked: {\r\n      type: \"datetime\",\r\n      columnName: \"last_checked\",\r\n      defaultsTo: null\r\n    },\r\n    period: {\r\n      type: \"integer\"\r\n    },\r\n    actionType: {\r\n      columnName: \"action_type\",\r\n      model: \"ActionType\",\r\n      required: true\r\n    }\r\n  }\r\n}));\r\n\r\nwaterline.loadCollection(Waterline.Collection.extend({\r\n  identity: 'ActionType',\r\n  tableName: 'action_type',\r\n  connection: 'postgres',\r\n\r\n  schema: true,\r\n\r\n  attributes: {\r\n    name: {\r\n      type: \"string\"\r\n    },\r\n    shortName: {\r\n      type: \"string\",\r\n      columnName: \"short_name\"\r\n    }\r\n  },\r\n  validationMessages: {}\r\n}));\r\n\r\nwaterline.initialize(config, function(error, models) {\r\n  if (error) {\r\n    throw error;\r\n  }\r\n\r\n  models.collections.action.find({\r\n    'actionType': {\r\n      name: '1'\r\n    }\r\n  })\r\n    .populate('actionType')\r\n    .exec(console.log);\r\n});\r\n```"},"comment":{"url":"https://api.github.com/repos/balderdashy/waterline-sequel/issues/comments/205718305","html_url":"https://github.com/balderdashy/waterline-sequel/issues/82#issuecomment-205718305","issue_url":"https://api.github.com/repos/balderdashy/waterline-sequel/issues/82","id":205718305,"user":{"login":"jaroslav-muller","id":11649028,"avatar_url":"https://avatars.githubusercontent.com/u/11649028?v=3","gravatar_id":"","url":"https://api.github.com/users/jaroslav-muller","html_url":"https://github.com/jaroslav-muller","followers_url":"https://api.github.com/users/jaroslav-muller/followers","following_url":"https://api.github.com/users/jaroslav-muller/following{/other_user}","gists_url":"https://api.github.com/users/jaroslav-muller/gists{/gist_id}","starred_url":"https://api.github.com/users/jaroslav-muller/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jaroslav-muller/subscriptions","organizations_url":"https://api.github.com/users/jaroslav-muller/orgs","repos_url":"https://api.github.com/users/jaroslav-muller/repos","events_url":"https://api.github.com/users/jaroslav-muller/events{/privacy}","received_events_url":"https://api.github.com/users/jaroslav-muller/received_events","type":"User","site_admin":false},"created_at":"2016-04-05T08:34:42Z","updated_at":"2016-04-05T08:34:42Z","body":"This is still reproducible and it definitely feels like a bug.\r\nPlease, don't close it unless you either fix it or convince me it's not a bug."}}