{"action":"created","issue":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/7600","labels_url":"https://api.github.com/repos/cakephp/cakephp/issues/7600/labels{/name}","comments_url":"https://api.github.com/repos/cakephp/cakephp/issues/7600/comments","events_url":"https://api.github.com/repos/cakephp/cakephp/issues/7600/events","html_url":"https://github.com/cakephp/cakephp/pull/7600","id":113011188,"number":7600,"title":"Set the foreignKey of the (source hasMany junction) association.","user":{"login":"QuanticPotato","id":7755443,"avatar_url":"https://avatars.githubusercontent.com/u/7755443?v=3","gravatar_id":"","url":"https://api.github.com/users/QuanticPotato","html_url":"https://github.com/QuanticPotato","followers_url":"https://api.github.com/users/QuanticPotato/followers","following_url":"https://api.github.com/users/QuanticPotato/following{/other_user}","gists_url":"https://api.github.com/users/QuanticPotato/gists{/gist_id}","starred_url":"https://api.github.com/users/QuanticPotato/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuanticPotato/subscriptions","organizations_url":"https://api.github.com/users/QuanticPotato/orgs","repos_url":"https://api.github.com/users/QuanticPotato/repos","events_url":"https://api.github.com/users/QuanticPotato/events{/privacy}","received_events_url":"https://api.github.com/users/QuanticPotato/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/cakephp/cakephp/labels/ORM","name":"ORM","color":"ffffff"}],"state":"open","locked":false,"assignee":null,"milestone":{"url":"https://api.github.com/repos/cakephp/cakephp/milestones/64","html_url":"https://github.com/cakephp/cakephp/milestones/3.1.3","labels_url":"https://api.github.com/repos/cakephp/cakephp/milestones/64/labels","id":1362733,"number":64,"title":"3.1.3","description":"Bugfixes and minor features for the 3.1.x release branch","creator":{"login":"markstory","id":24086,"avatar_url":"https://avatars.githubusercontent.com/u/24086?v=3","gravatar_id":"","url":"https://api.github.com/users/markstory","html_url":"https://github.com/markstory","followers_url":"https://api.github.com/users/markstory/followers","following_url":"https://api.github.com/users/markstory/following{/other_user}","gists_url":"https://api.github.com/users/markstory/gists{/gist_id}","starred_url":"https://api.github.com/users/markstory/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/markstory/subscriptions","organizations_url":"https://api.github.com/users/markstory/orgs","repos_url":"https://api.github.com/users/markstory/repos","events_url":"https://api.github.com/users/markstory/events{/privacy}","received_events_url":"https://api.github.com/users/markstory/received_events","type":"User","site_admin":false},"open_issues":21,"closed_issues":18,"state":"open","created_at":"2015-10-19T01:08:03Z","updated_at":"2015-10-24T01:35:10Z","due_on":"2015-11-01T04:00:00Z","closed_at":null},"comments":4,"created_at":"2015-10-23T12:38:17Z","updated_at":"2015-10-24T10:34:03Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/cakephp/cakephp/pulls/7600","html_url":"https://github.com/cakephp/cakephp/pull/7600","diff_url":"https://github.com/cakephp/cakephp/pull/7600.diff","patch_url":"https://github.com/cakephp/cakephp/pull/7600.patch"},"body":"In application with complex associations, the foreignKey might be null in the (source hasMany junction) association (in a BelongsToMany association).\r\nThen, a default foreignKey is generated using the table name, but it is not the correct key. It then leads to database error."},"comment":{"url":"https://api.github.com/repos/cakephp/cakephp/issues/comments/150782696","html_url":"https://github.com/cakephp/cakephp/pull/7600#issuecomment-150782696","issue_url":"https://api.github.com/repos/cakephp/cakephp/issues/7600","id":150782696,"user":{"login":"QuanticPotato","id":7755443,"avatar_url":"https://avatars.githubusercontent.com/u/7755443?v=3","gravatar_id":"","url":"https://api.github.com/users/QuanticPotato","html_url":"https://github.com/QuanticPotato","followers_url":"https://api.github.com/users/QuanticPotato/followers","following_url":"https://api.github.com/users/QuanticPotato/following{/other_user}","gists_url":"https://api.github.com/users/QuanticPotato/gists{/gist_id}","starred_url":"https://api.github.com/users/QuanticPotato/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/QuanticPotato/subscriptions","organizations_url":"https://api.github.com/users/QuanticPotato/orgs","repos_url":"https://api.github.com/users/QuanticPotato/repos","events_url":"https://api.github.com/users/QuanticPotato/events{/privacy}","received_events_url":"https://api.github.com/users/QuanticPotato/received_events","type":"User","site_admin":false},"created_at":"2015-10-24T10:34:03Z","updated_at":"2015-10-24T10:34:03Z","body":"I would really like to create a TestCase, but I'm pretty new with this.\r\n\r\nBasically, the error happens with reciprocal associations : \r\n    Articles belongsToMany Tags\r\n    Tags belongsToMany Articles\r\n\r\nThese associations are usual : \r\nsrc/Model/Table/ArticlesTable.php :\r\n```\r\n$this->belongsToMany('Tags', [\r\n    'joinTable' => 'articles_tags',\r\n    'foreignKey' => 'article',\r\n    'targetForeignKey' => 'tag'\r\n]);\r\n```\r\nsrc/Model/Table/TagsTable : \r\n```\r\n$this->belongsToMany('Articles', [\r\n    'joinTable' => 'articles_tags',\r\n    'foreignKey' => 'tag',\r\n    'targetForeignKey' => 'article'\r\n]);\r\n```\r\n\r\nThen, if you perform two request in two different directions, the second fails : \r\n```\r\n// In a controller ...\r\n$Articles = $this->loadModel('Articles');\r\n$Tags = $this->loadModel('Tags');\r\n\r\n$req1 = $Articles->get(1, ['contain' => ['Tags']]);\r\n$req2 = $Tags->get(1, ['contain' => ['Articles']]);\r\n```\r\nThe problem is that the foreignKey of the involved associations (hasMany) is not valid (it is null, and then a default one is generated : \"<model>_id\", which is not correct with custom foreignKeys).\r\n\r\nI don't really know where to write the TestCase, because BelongsToManyTest.php perform tests on Mock Tables (and then I can't reach the error)."}}