{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/668","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/668/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/668/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/668/events","html_url":"https://github.com/tgriesser/bookshelf/issues/668","id":64068834,"number":668,"title":"#format is extremely confusing","user":{"login":"jclem","id":412282,"avatar_url":"https://avatars.githubusercontent.com/u/412282?v=3","gravatar_id":"","url":"https://api.github.com/users/jclem","html_url":"https://github.com/jclem","followers_url":"https://api.github.com/users/jclem/followers","following_url":"https://api.github.com/users/jclem/following{/other_user}","gists_url":"https://api.github.com/users/jclem/gists{/gist_id}","starred_url":"https://api.github.com/users/jclem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jclem/subscriptions","organizations_url":"https://api.github.com/users/jclem/orgs","repos_url":"https://api.github.com/users/jclem/repos","events_url":"https://api.github.com/users/jclem/events{/privacy}","received_events_url":"https://api.github.com/users/jclem/received_events","type":"User","site_admin":false},"labels":[],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":2,"created_at":"2015-03-24T18:36:24Z","updated_at":"2015-03-24T18:45:46Z","closed_at":null,"body":"**tl;dr** Basically, I'm saying that only primary keys should be used to query when possible, and that we need separate methods for formatting prior to persistence and for transforming property names.\r\n\r\nI see this issue come up a *lot* with others using Bookshelf, so I think it warrants more discussion. This is somewhat related to https://github.com/tgriesser/bookshelf/issues/550 and a few other issues.\r\n\r\nThe documentation for `#format` says that it is \"used to modify the current state of the model before it is persisted to the database\". There's no indication that calling `#format` affects the query used to do things like fetch models. However, `#format` still appears to be called when doing things like calling `model.fetch()`, and it also unexpectedly breaks calling `load`.\r\n\r\nFor example, if my `#format` method is this:\r\n\r\n```javascript\r\nformat: function format(attributes) {\r\n  return _.pick(attributes, 'name');\r\n}\r\n```\r\n\r\nAccording to documentation, I would expect the only affect this has to be that only the `name` attribute is serialized before persisting a model to the database.\r\n\r\nHowever, this also means that calling `#fetch` will only use the `name` attribute, and never `id`, and that calling `model.related(['widgets'])` on a hasMany relationship will result in a query like this:\r\n\r\n```javascript\r\n{\r\n  bindings: [undefined],\r\n  sql: 'select \"widgets\".* from \"widgets\" where \"widgets\".\"model_id\" in ($1)'\r\n}\r\n```\r\n\r\nIt took me a *long* time to realize that this was because of my `#format` method. It *really* seems like I should be able to tell Bookshelf that `id` is my primary key, and that it is therefore the *only* column that needs to be used for querying a given model unless I specify otherwise.\r\n\r\nThis is one of the few places where Bookshelf does something totally unexpected in the context of other popular ORMs, so it might be worth considering a change here."},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/85638820","html_url":"https://github.com/tgriesser/bookshelf/issues/668#issuecomment-85638820","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/668","id":85638820,"user":{"login":"jclem","id":412282,"avatar_url":"https://avatars.githubusercontent.com/u/412282?v=3","gravatar_id":"","url":"https://api.github.com/users/jclem","html_url":"https://github.com/jclem","followers_url":"https://api.github.com/users/jclem/followers","following_url":"https://api.github.com/users/jclem/following{/other_user}","gists_url":"https://api.github.com/users/jclem/gists{/gist_id}","starred_url":"https://api.github.com/users/jclem/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/jclem/subscriptions","organizations_url":"https://api.github.com/users/jclem/orgs","repos_url":"https://api.github.com/users/jclem/repos","events_url":"https://api.github.com/users/jclem/events{/privacy}","received_events_url":"https://api.github.com/users/jclem/received_events","type":"User","site_admin":false},"created_at":"2015-03-24T18:45:46Z","updated_at":"2015-03-24T18:45:46Z","body":"Thanks @tgriesser!"}}