{"action":"created","issue":{"url":"https://api.github.com/repos/balderdashy/waterline/issues/788","labels_url":"https://api.github.com/repos/balderdashy/waterline/issues/788/labels{/name}","comments_url":"https://api.github.com/repos/balderdashy/waterline/issues/788/comments","events_url":"https://api.github.com/repos/balderdashy/waterline/issues/788/events","html_url":"https://github.com/balderdashy/waterline/issues/788","id":53811384,"number":788,"title":"Associations named 'type' break other columns","user":{"login":"bradshja","id":9354010,"avatar_url":"https://avatars.githubusercontent.com/u/9354010?v=3","gravatar_id":"","url":"https://api.github.com/users/bradshja","html_url":"https://github.com/bradshja","followers_url":"https://api.github.com/users/bradshja/followers","following_url":"https://api.github.com/users/bradshja/following{/other_user}","gists_url":"https://api.github.com/users/bradshja/gists{/gist_id}","starred_url":"https://api.github.com/users/bradshja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradshja/subscriptions","organizations_url":"https://api.github.com/users/bradshja/orgs","repos_url":"https://api.github.com/users/bradshja/repos","events_url":"https://api.github.com/users/bradshja/events{/privacy}","received_events_url":"https://api.github.com/users/bradshja/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/balderdashy/waterline/labels/bug","name":"bug","color":"e11d21"},{"url":"https://api.github.com/repos/balderdashy/waterline/labels/needs+fix","name":"needs fix","color":"eb6420"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":6,"created_at":"2015-01-08T22:46:00Z","updated_at":"2015-01-09T15:46:29Z","closed_at":null,"body":"Creating an association named 'type' (maybe only belongsTo associations) causes other columns to misbehave by including the association's data.  As an example, I have models like this:\r\n\r\nAccount.js:\r\n```\r\nmodule.exports = {\r\n    ...\r\n    attributes: {\r\n        account_id: {\r\n            type: 'integer',\r\n            autoIncrement: true,\r\n            primaryKey: true\r\n        },\r\n        type: {\r\n            model: 'accounttype',\r\n            columnName: 'type_id'\r\n        },\r\n       devices: {\r\n            model: 'device',\r\n            via: 'account'\r\n       }\r\n    }\r\n};\r\n```\r\nAccountType.js:\r\n```\r\nmodule.exports = {\r\n    ...\r\n    attributes: {\r\n        type_id: {\r\n            type: 'integer',\r\n            autoIncrement: true,\r\n            primaryKey: true\r\n        },\r\n        type: {\r\n           type: 'string'\r\n        }\r\n    }\r\n};\r\n```\r\nDevice.js:\r\n```\r\nmodule.exports = {\r\n    ...\r\n    attributes: {\r\n        device_id: {\r\n            type: 'integer',\r\n            autoIncrement: true,\r\n            primaryKey: true\r\n        },\r\n        account: {\r\n            model: 'account',\r\n            columnName: 'account_id'\r\n        },\r\n       mac_address: {\r\n            type: 'string'\r\n       }\r\n    }\r\n};\r\n```\r\n\r\nGiven those models, if I retrieve an account with the API, I expect to get something like this:\r\n```\r\n{\r\n  \"devices\": [ \r\n     {\r\n      \"device_id\": 561665222302,\r\n      \"mac_address\": \"00:02:02:4f:b1:59\",\r\n      \"account_id\": 105149\r\n    }\r\n  ],\r\n  \"type\": {\r\n    \"type_id\": 8,\r\n    \"type\": \"Employee\"\r\n  },\r\n  \"status\": {\r\n    \"status_id\": 1,\r\n    \"status\": \"Active\"\r\n  },\r\n  \"account_id\": 105149,\r\n  \"display_name\": \"Jordan Bradshaw\"\r\n}\r\n```\r\nbut I get this instead:\r\n```\r\n{\r\n  \"devices\": [],\r\n  \"type\": {\r\n    \"type_id\": 8,\r\n    \"type\": \"Employee\"\r\n  },\r\n  \"status\": {\r\n    \"status_id\": 1,\r\n    \"status\": \"Active\"\r\n  },\r\n  \"account_id\": [\r\n    {\r\n      \"device_id\": 561665222302,\r\n      \"mac_address\": \"00:02:02:4f:b1:59\",\r\n      \"account_id\": 105149\r\n    }\r\n  ],\r\n  \"display_name\": \"Jordan Bradshaw\"\r\n}\r\n```\r\nAs you can see, the devices association is populating the account_id property, which is supposed to just be a numerical column as the primary key of the accounts collection.\r\n\r\nAfter much investigation, I've discovered that this is caused by the Transformation.serialize function being applied to the collection's schema in waterline/core/index.js on line 104.  The serialization function sees the type column and recursively replaces all instances of 'type' with 'type_id.'  I'm not sure how this causes the wackiness above, but removing the serialization line fixes the problem, at the possible expense of causing problems elsewhere that I haven't seen.\r\n\r\nIt doesn't look like Transformation.serialize can be made non recursive since it's used in other places, so some other function might should be used to transform the collection's schema.  If I had to guess, this probably prevents any association that uses reserved field names from working right.  'type', 'primaryKey', 'model', 'collection' and so on probably don't work either."},"comment":{"url":"https://api.github.com/repos/balderdashy/waterline/issues/comments/69351571","html_url":"https://github.com/balderdashy/waterline/issues/788#issuecomment-69351571","issue_url":"https://api.github.com/repos/balderdashy/waterline/issues/788","id":69351571,"user":{"login":"bradshja","id":9354010,"avatar_url":"https://avatars.githubusercontent.com/u/9354010?v=3","gravatar_id":"","url":"https://api.github.com/users/bradshja","html_url":"https://github.com/bradshja","followers_url":"https://api.github.com/users/bradshja/followers","following_url":"https://api.github.com/users/bradshja/following{/other_user}","gists_url":"https://api.github.com/users/bradshja/gists{/gist_id}","starred_url":"https://api.github.com/users/bradshja/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/bradshja/subscriptions","organizations_url":"https://api.github.com/users/bradshja/orgs","repos_url":"https://api.github.com/users/bradshja/repos","events_url":"https://api.github.com/users/bradshja/events{/privacy}","received_events_url":"https://api.github.com/users/bradshja/received_events","type":"User","site_admin":false},"created_at":"2015-01-09T15:46:29Z","updated_at":"2015-01-09T15:46:29Z","body":"I just submitted pull request #789 that fixes my case at least.  I ended up adding some small changes to Transformation.serialize that lets you restrict the depth that it serializes to (no max by default).  By limiting the depth of serialization to 1 level in lib/core/index.js, the types of other columns are no longer clobbered by the 'type' association's columnName.\r\n\r\nIt's untested in general, but it probably fixes other related cases too."}}