{"action":"created","issue":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/71","repository_url":"https://api.github.com/repos/tgriesser/bookshelf","labels_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/71/labels{/name}","comments_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/71/comments","events_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/71/events","html_url":"https://github.com/tgriesser/bookshelf/issues/71","id":20095856,"number":71,"title":"Attaching relations ","user":{"login":"cstotzer","id":3476592,"avatar_url":"https://avatars.githubusercontent.com/u/3476592?v=3","gravatar_id":"","url":"https://api.github.com/users/cstotzer","html_url":"https://github.com/cstotzer","followers_url":"https://api.github.com/users/cstotzer/followers","following_url":"https://api.github.com/users/cstotzer/following{/other_user}","gists_url":"https://api.github.com/users/cstotzer/gists{/gist_id}","starred_url":"https://api.github.com/users/cstotzer/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/cstotzer/subscriptions","organizations_url":"https://api.github.com/users/cstotzer/orgs","repos_url":"https://api.github.com/users/cstotzer/repos","events_url":"https://api.github.com/users/cstotzer/events{/privacy}","received_events_url":"https://api.github.com/users/cstotzer/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/tgriesser/bookshelf/labels/may%20revisit","name":"may revisit","color":"d4c5f9"},{"url":"https://api.github.com/repos/tgriesser/bookshelf/labels/question","name":"question","color":"cc317c"}],"state":"open","locked":false,"assignee":null,"milestone":null,"comments":19,"created_at":"2013-09-26T08:20:38Z","updated_at":"2016-02-06T04:48:00Z","closed_at":null,"body":"I'm pretty sure that I've got something terribly wrong. I've got the following models: \r\n\r\n```\r\nvar Project = bookshelfInstance.Model.extend({\r\n    tableName: 'projects',\r\n    tasks: function () {\r\n        return this.hasMany(Task)\r\n    }\r\n})\r\n\r\nvar Task = bookshelfInstance.Model.extend({\r\n    tableName: 'tasks',\r\n    project: function () {\r\n        return this.belongsTo(Project)\r\n    }\r\n})\r\n```\r\nI could not figure how to save a task with the relation to projects set, here's whart I'm doing:\r\n```\r\nvar task = new Task({description: 'Just do it!'})\r\n\r\nProject.forge({id: 1}).fetch()\r\n    .then(\r\n    function (project) {\r\n        console.log(project)\r\n        return when(task.related('project').set(project)).yield(task)\r\n    })\r\n    .then(function (task) {\r\n        console.log(task)\r\n        return task.save()\r\n    })\r\n    .otherwise(function (error) {\r\n        console.log(error)\r\n    })\r\n```\r\nPostgres then answers with a constraint violation since project id on `tasks` id defined as `NOT NULL`:\r\n```\r\n/usr/local/bin/node --debug-brk=62219 bookshelf-playground.js\r\ndebugger listening on port 62219\r\n{ sql: 'select \"projects\".* from \"projects\" where \"id\" = ? limit 1',\r\n  bindings: [ 1 ],\r\n  __cid: '__cid3' }\r\n[ 'Project',\r\n  { attributes: { id: 1, description: 'PROJECT A' },\r\n    _previousAttributes: { id: 1, description: 'PROJECT A' },\r\n    changed: {},\r\n    relations: {},\r\n    cid: 'c2',\r\n    id: 1,\r\n    _handleResponse: [Function],\r\n    _handleEager: [Function] } ]\r\n[ 'Task: ',\r\n  { attributes: { description: 'Just do it!' },\r\n    _previousAttributes: {},\r\n    changed: { description: 'Just do it!' },\r\n    relations: { project: [Object] },\r\n    cid: 'c1',\r\n    _handleResponse: [Function],\r\n    _handleEager: [Function] } ]\r\n{ sql: 'insert into \"tasks\" (\"description\") values (?) returning \"id\"',\r\n  bindings: [ 'Just do it!' ],\r\n  __cid: '__cid4' }\r\n{ [error: null value in column \"project_id\" violates not-null constraint]\r\n  length: 118,\r\n  name: 'error',\r\n  severity: 'ERROR',\r\n  code: '23502',\r\n  detail: undefined,\r\n  hint: undefined,\r\n  position: undefined,\r\n  internalPosition: undefined,\r\n  internalQuery: undefined,\r\n  where: undefined,\r\n  file: 'execMain.c',\r\n  line: '1572',\r\n  routine: 'ExecConstraints' }\r\n```\r\n# Test case:\r\n## schema.sql\r\n```\r\nDROP TABLE tasks;\r\nDROP TABLE projects;\r\n\r\nCREATE TABLE projects (\r\n  id SERIAL NOT NULL,\r\n  description CHARACTER VARYING(64),\r\n  CONSTRAINT projects_pkey PRIMARY KEY (id)\r\n);\r\n\r\nCREATE TABLE tasks (\r\n  id SERIAL NOT NULL,\r\n  project_id INTEGER NOT NULL,\r\n  description CHARACTER VARYING(256),\r\n  CONSTRAINT tasks_pkey PRIMARY KEY (id)\r\n);\r\n\r\nALTER TABLE tasks ADD CONSTRAINT tasks_projects_fk FOREIGN KEY (project_id) REFERENCES projects (id);\r\n\r\nINSERT INTO projects (description) VALUES ('PROJECT A');\r\nINSERT INTO projects (description) VALUES ('PROJECT B');\r\n```\r\n## app.js\r\n```\r\nvar Bookshelf = require('bookshelf'),\r\n    when = require('when')\r\n\r\nbookshelfInstance = Bookshelf.initialize({\r\n    client: 'postgres',\r\n    connection: {\r\n        host: 'localhost',\r\n        user: '***',\r\n        database: 'playground',\r\n        charset: 'utf8'\r\n    },\r\n    debug: true\r\n})\r\n\r\nvar Project = bookshelfInstance.Model.extend({\r\n    tableName: 'projects',\r\n    tasks: function () {\r\n        return this.hasMany(Task)\r\n    }\r\n})\r\n\r\nvar Task = bookshelfInstance.Model.extend({\r\n    tableName: 'tasks',\r\n    project: function () {\r\n        return this.belongsTo(Project)\r\n    }\r\n})\r\n\r\nvar task = new Task({description: 'Just do it!'})\r\n\r\nProject.forge({id: 1}).fetch()\r\n    .then(\r\n    function (project) {\r\n        console.log(['Project', project])\r\n        return when(task.related('project').set(project)).yield(task)\r\n    })\r\n    .then(function (task) {\r\n        console.log(['Task: ', task])\r\n        return task.save()\r\n    })\r\n    .otherwise(function (error) {\r\n        console.log(error)\r\n    })\r\n```"},"comment":{"url":"https://api.github.com/repos/tgriesser/bookshelf/issues/comments/180680935","html_url":"https://github.com/tgriesser/bookshelf/issues/71#issuecomment-180680935","issue_url":"https://api.github.com/repos/tgriesser/bookshelf/issues/71","id":180680935,"user":{"login":"rhys-vdw","id":822105,"avatar_url":"https://avatars.githubusercontent.com/u/822105?v=3","gravatar_id":"","url":"https://api.github.com/users/rhys-vdw","html_url":"https://github.com/rhys-vdw","followers_url":"https://api.github.com/users/rhys-vdw/followers","following_url":"https://api.github.com/users/rhys-vdw/following{/other_user}","gists_url":"https://api.github.com/users/rhys-vdw/gists{/gist_id}","starred_url":"https://api.github.com/users/rhys-vdw/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rhys-vdw/subscriptions","organizations_url":"https://api.github.com/users/rhys-vdw/orgs","repos_url":"https://api.github.com/users/rhys-vdw/repos","events_url":"https://api.github.com/users/rhys-vdw/events{/privacy}","received_events_url":"https://api.github.com/users/rhys-vdw/received_events","type":"User","site_admin":false},"created_at":"2016-02-06T04:48:00Z","updated_at":"2016-02-06T04:48:00Z","body":"@syzer @kichooo Did my instructions not work? Not provide the functionality you want? What exactly are you asking for?"}}