{"id":1580,"date":"2022-08-30T15:17:43","date_gmt":"2022-08-30T15:17:43","guid":{"rendered":"https:\/\/unknownerror.org\/index.php\/2013\/11\/27\/kohana-3-2-orm-many-to-many-wrong-field-names-collection-of-common-programming-errors\/"},"modified":"2022-08-30T15:17:43","modified_gmt":"2022-08-30T15:17:43","slug":"kohana-3-2-orm-many-to-many-wrong-field-names-collection-of-common-programming-errors","status":"publish","type":"post","link":"https:\/\/unknownerror.org\/index.php\/2022\/08\/30\/kohana-3-2-orm-many-to-many-wrong-field-names-collection-of-common-programming-errors\/","title":{"rendered":"Kohana 3.2 ORM many-to-many &#8211; wrong field names-Collection of common programming errors"},"content":{"rendered":"<p>I trying to make many-to-many relation between 2 models: Users_Role and Users_Right<\/p>\n<pre><code>class Model_Users_Role extends ORM{\n    protected $_has_many = array(\n        'rights' =&gt; array(\n            'model'   =&gt; 'users_right',\n            'through' =&gt; 'users_roles_rights',\n        ),\n    ); \n}\n\nclass Model_Users_Right extends ORM{    \n    protected $_has_many = array(\n        'roles' =&gt; array(\n            'model'   =&gt; 'users_role',\n            'through' =&gt; 'users_roles_rights',\n        ),\n    );\n}\n<\/code><\/pre>\n<p>I&#8217;m trying to do this:<\/p>\n<pre><code>$role = ORM::factory('users_role', 1);\n$right = ORM::factory('users_right', 1);\n$right-&gt;add('roles', $role);\n<\/code><\/pre>\n<p>Error:<\/p>\n<pre><code>Database_Exception [ 1054 ]: Unknown column 'role_id' in 'field list' [ INSERT INTO `users_roles_rights` (`users_right_id`, `role_id`) VALUES ('1', '1') ]\n<\/code><\/pre>\n<p>I tryed to make it on the other side:<\/p>\n<pre><code>$role = ORM::factory('users_role', 1);\n$right = ORM::factory('users_right', 1);        \n$role-&gt;add('rights', $right);\n<\/code><\/pre>\n<p>New error:<\/p>\n<pre><code>Database_Exception [ 1054 ]: Unknown column 'right_id' in 'field list' [ INSERT INTO `users_roles_rights` (`users_role_id`, `right_id`) VALUES ('1', '1') ]\n<\/code><\/pre>\n<p>I expected ORM to use <code>users_role_id<\/code> and <code>users_right_id<\/code> field names in pivot table, but it uses wrong name of far key? Where I have made a mistake?<\/p>\n<ol>\n<li>\n<p>Check out where the default values are set. Try this:<\/p>\n<pre><code>class Model_Users_Role extends ORM{\n    protected $_has_many = array(\n        'rights' =&gt; array(\n            'model'   =&gt; 'users_right',\n            'far_key' =&gt; 'users_right_id',\n            'through' =&gt; 'users_roles_rights',\n        ),\n    ); \n}\n\nclass Model_Users_Right extends ORM{    \n    protected $_has_many = array(\n        'roles' =&gt; array(\n            'model'   =&gt; 'users_role',\n            'far_key' =&gt; 'users_role_id',\n            'through' =&gt; 'users_roles_rights',\n        ),\n    );\n}\n<\/code><\/pre>\n<p>Kohana does not check that the relationship does not already exist.<\/p>\n<p>Either enforce this in the database table with a composite unique key on the two foreign key columns, or make your code check that the relationship does not already exist before adding.<\/p>\n<\/li>\n<\/ol>\n<p id=\"rop\"><small>Originally posted 2013-11-27 11:53:26. <\/small><\/p>","protected":false},"excerpt":{"rendered":"<p>I trying to make many-to-many relation between 2 models: Users_Role and Users_Right class Model_Users_Role extends ORM{ protected $_has_many = array( &#8216;rights&#8217; =&gt; array( &#8216;model&#8217; =&gt; &#8216;users_right&#8217;, &#8216;through&#8217; =&gt; &#8216;users_roles_rights&#8217;, ), ); } class Model_Users_Right extends ORM{ protected $_has_many = array( &#8216;roles&#8217; =&gt; array( &#8216;model&#8217; =&gt; &#8216;users_role&#8217;, &#8216;through&#8217; =&gt; &#8216;users_roles_rights&#8217;, ), ); } I&#8217;m trying to [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[],"class_list":["post-1580","post","type-post","status-publish","format-standard","hentry","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1580","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/comments?post=1580"}],"version-history":[{"count":0,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/posts\/1580\/revisions"}],"wp:attachment":[{"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/media?parent=1580"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/categories?post=1580"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/unknownerror.org\/index.php\/wp-json\/wp\/v2\/tags?post=1580"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}