<?xml version="1.0" encoding="UTF-8"?>
<ticket>
  <assigned-user-id type="integer">141</assigned-user-id>
  <attachments-count type="integer">3</attachments-count>
  <closed type="boolean">true</closed>
  <created-at type="datetime">2009-02-26T16:24:56+00:00</created-at>
  <creator-id type="integer">47440</creator-id>
  <milestone-due-on type="datetime">2009-08-31T00:00:00+01:00</milestone-due-on>
  <milestone-id type="integer">41987</milestone-id>
  <number type="integer">2086</number>
  <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
  <priority type="integer">11</priority>
  <project-id type="integer">8994</project-id>
  <raw-data type="binary" nil="true" encoding="base64"></raw-data>
  <state>committed</state>
  <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
  <title>Primary key on HABTM join table now raises an exception</title>
  <updated-at type="datetime">2009-09-27T18:15:48+01:00</updated-at>
  <user-id type="integer">71184</user-id>
  <user-name>tsenart</user-name>
  <creator-name>Jaime Bellmyer</creator-name>
  <assigned-user-name>Michael Koziarski</assigned-user-name>
  <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
  <milestone-title>2.3.4</milestone-title>
  <original-body>Join tables for has_and_belongs_to_many relationships are not supposed to have primary keys.  This is mentioned in a few places in the documentation, but remains an easy &quot;gotcha&quot;.  Unless you know this rule, you can create several habtm relationships before the process eventually fails in a cryptic manner.  It's very likely that your tests will not catch this, since it does not fail right away.

The database eventually chokes on duplicate primary keys.  The root cause of the problem is that, excluding foreign keys, ActiveRecord attempts to copy values from the associated table into the join table for any columns that the two tables have in common.  If you leave the primary key in your join table, 'id' is usually one of those columns.

I've attached a patch with my solution, which includes full testing for all of my changes.  When you attempt to create a habtm association, an exception is raised if the join table contains a primary key.  This means it will fail right away, loudly, and with a clear explanation of the problem.  The exception class raised is ActiveRecord::ConfigurationError, and the message is:

&quot;Primary key is not allowed in a has_and_belongs_to_many join table (table_name).&quot;

In order to create this solution, I had to create a unified way for the various connection adapters to return a primary key, even for tables that are not connected directly to an ActiveRecord model.  This is usually the case with basic join tables.  sqlite's adapter has a primary_key(table_name) method, which I mimicked in the mysql and postgresql adapters.

In order to handle new adapters gracefully, I created a supports_primary_key? method in AbstractAdapter (similar to supports_migrations?) that returns false.  It is then overridden in the specific connection adapters (mysql, sqlite, and postgresql at this time) to return true, since those adapters now have a primary_key method that accepts a table name and returns the primary key. The primary_key method returns nil if there is no primary key for that table.

This patch replaces my earlier attempt to solve the problem in lighthouse ticket #2068.  That patch made it possible to *keep* primary keys in your join tables and eliminated the db failure.  But I have since learned this is not the solution the community wants.</original-body>
  <latest-body>Join tables for has_and_belongs_to_many relationships are not supposed to have primary keys.  This is mentioned in a few places in the documentation, but remains an easy &quot;gotcha&quot;.  Unless you know this rule, you can create several habtm relationships before the process eventually fails in a cryptic manner.  It's very likely that your tests will not catch this, since it does not fail right away.

The database eventually chokes on duplicate primary keys.  The root cause of the problem is that, excluding foreign keys, ActiveRecord attempts to copy values from the associated table into the join table for any columns that the two tables have in common.  If you leave the primary key in your join table, 'id' is usually one of those columns.

I've attached a patch with my solution, which includes full testing for all of my changes.  When you attempt to create a habtm association, an exception is raised if the join table contains a primary key.  This means it will fail right away, loudly, and with a clear explanation of the problem.  The exception class raised is ActiveRecord::ConfigurationError, and the message is:

&quot;Primary key is not allowed in a has_and_belongs_to_many join table (table_name).&quot;

In order to create this solution, I had to create a unified way for the various connection adapters to return a primary key, even for tables that are not connected directly to an ActiveRecord model.  This is usually the case with basic join tables.  sqlite's adapter has a primary_key(table_name) method, which I mimicked in the mysql and postgresql adapters.

In order to handle new adapters gracefully, I created a supports_primary_key? method in AbstractAdapter (similar to supports_migrations?) that returns false.  It is then overridden in the specific connection adapters (mysql, sqlite, and postgresql at this time) to return true, since those adapters now have a primary_key method that accepts a table name and returns the primary key. The primary_key method returns nil if there is no primary key for that table.

This patch replaces my earlier attempt to solve the problem in lighthouse ticket #2068.  That patch made it possible to *keep* primary keys in your join tables and eliminated the db failure.  But I have since learned this is not the solution the community wants.</latest-body>
  <original-body-html>&lt;div&gt;&lt;p&gt;Join tables for has_and_belongs_to_many relationships are not
supposed to have primary keys. This is mentioned in a few places in
the documentation, but remains an easy &quot;gotcha&quot;. Unless you know
this rule, you can create several habtm relationships before the
process eventually fails in a cryptic manner. It's very likely that
your tests will not catch this, since it does not fail right
away.&lt;/p&gt;
&lt;p&gt;The database eventually chokes on duplicate primary keys. The
root cause of the problem is that, excluding foreign keys,
ActiveRecord attempts to copy values from the associated table into
the join table for any columns that the two tables have in common.
If you leave the primary key in your join table, 'id' is usually
one of those columns.&lt;/p&gt;
&lt;p&gt;I've attached a patch with my solution, which includes full
testing for all of my changes. When you attempt to create a habtm
association, an exception is raised if the join table contains a
primary key. This means it will fail right away, loudly, and with a
clear explanation of the problem. The exception class raised is
ActiveRecord::ConfigurationError, and the message is:&lt;/p&gt;
&lt;p&gt;&quot;Primary key is not allowed in a has_and_belongs_to_many join
table (table_name).&quot;&lt;/p&gt;
&lt;p&gt;In order to create this solution, I had to create a unified way
for the various connection adapters to return a primary key, even
for tables that are not connected directly to an ActiveRecord
model. This is usually the case with basic join tables. sqlite's
adapter has a primary_key(table_name) method, which I mimicked in
the mysql and postgresql adapters.&lt;/p&gt;
&lt;p&gt;In order to handle new adapters gracefully, I created a
supports_primary_key? method in AbstractAdapter (similar to
supports_migrations?) that returns false. It is then overridden in
the specific connection adapters (mysql, sqlite, and postgresql at
this time) to return true, since those adapters now have a
primary_key method that accepts a table name and returns the
primary key. The primary_key method returns nil if there is no
primary key for that table.&lt;/p&gt;
&lt;p&gt;This patch replaces my earlier attempt to solve the problem in
lighthouse ticket &lt;a href=&quot;/projects/8994/tickets/2068&quot; title=&quot;Ticket #2068&quot;&gt;#2068&lt;/a&gt;. That patch made it possible to
&lt;em&gt;keep&lt;/em&gt; primary keys in your join tables and eliminated the
db failure. But I have since learned this is not the solution the
community wants.&lt;/p&gt;&lt;/div&gt;</original-body-html>
  <versions type="array">
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">0</attachments-count>
      <body>Join tables for has_and_belongs_to_many relationships are not supposed to have primary keys.  This is mentioned in a few places in the documentation, but remains an easy &quot;gotcha&quot;.  Unless you know this rule, you can create several habtm relationships before the process eventually fails in a cryptic manner.  It's very likely that your tests will not catch this, since it does not fail right away.

The database eventually chokes on duplicate primary keys.  The root cause of the problem is that, excluding foreign keys, ActiveRecord attempts to copy values from the associated table into the join table for any columns that the two tables have in common.  If you leave the primary key in your join table, 'id' is usually one of those columns.

I've attached a patch with my solution, which includes full testing for all of my changes.  When you attempt to create a habtm association, an exception is raised if the join table contains a primary key.  This means it will fail right away, loudly, and with a clear explanation of the problem.  The exception class raised is ActiveRecord::ConfigurationError, and the message is:

&quot;Primary key is not allowed in a has_and_belongs_to_many join table (table_name).&quot;

In order to create this solution, I had to create a unified way for the various connection adapters to return a primary key, even for tables that are not connected directly to an ActiveRecord model.  This is usually the case with basic join tables.  sqlite's adapter has a primary_key(table_name) method, which I mimicked in the mysql and postgresql adapters.

In order to handle new adapters gracefully, I created a supports_primary_key? method in AbstractAdapter (similar to supports_migrations?) that returns false.  It is then overridden in the specific connection adapters (mysql, sqlite, and postgresql at this time) to return true, since those adapters now have a primary_key method that accepts a table name and returns the primary key. The primary_key method returns nil if there is no primary key for that table.

This patch replaces my earlier attempt to solve the problem in lighthouse ticket #2068.  That patch made it possible to *keep* primary keys in your join tables and eliminated the db failure.  But I have since learned this is not the solution the community wants.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Join tables for has_and_belongs_to_many relationships are not
supposed to have primary keys. This is mentioned in a few places in
the documentation, but remains an easy &quot;gotcha&quot;. Unless you know
this rule, you can create several habtm relationships before the
process eventually fails in a cryptic manner. It's very likely that
your tests will not catch this, since it does not fail right
away.&lt;/p&gt;
&lt;p&gt;The database eventually chokes on duplicate primary keys. The
root cause of the problem is that, excluding foreign keys,
ActiveRecord attempts to copy values from the associated table into
the join table for any columns that the two tables have in common.
If you leave the primary key in your join table, 'id' is usually
one of those columns.&lt;/p&gt;
&lt;p&gt;I've attached a patch with my solution, which includes full
testing for all of my changes. When you attempt to create a habtm
association, an exception is raised if the join table contains a
primary key. This means it will fail right away, loudly, and with a
clear explanation of the problem. The exception class raised is
ActiveRecord::ConfigurationError, and the message is:&lt;/p&gt;
&lt;p&gt;&quot;Primary key is not allowed in a has_and_belongs_to_many join
table (table_name).&quot;&lt;/p&gt;
&lt;p&gt;In order to create this solution, I had to create a unified way
for the various connection adapters to return a primary key, even
for tables that are not connected directly to an ActiveRecord
model. This is usually the case with basic join tables. sqlite's
adapter has a primary_key(table_name) method, which I mimicked in
the mysql and postgresql adapters.&lt;/p&gt;
&lt;p&gt;In order to handle new adapters gracefully, I created a
supports_primary_key? method in AbstractAdapter (similar to
supports_migrations?) that returns false. It is then overridden in
the specific connection adapters (mysql, sqlite, and postgresql at
this time) to return true, since those adapters now have a
primary_key method that accepts a table name and returns the
primary key. The primary_key method returns nil if there is no
primary key for that table.&lt;/p&gt;
&lt;p&gt;This patch replaces my earlier attempt to solve the problem in
lighthouse ticket &lt;a href=&quot;/projects/8994/tickets/2068&quot; title=&quot;Ticket #2068&quot;&gt;#2068&lt;/a&gt;. That patch made it possible to
&lt;em&gt;keep&lt;/em&gt; primary keys in your join tables and eliminated the
db failure. But I have since learned this is not the solution the
community wants.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-02-26T16:24:57+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>[PATCH] Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-02-26T16:24:59+00:00</updated-at>
      <user-id type="integer">47440</user-id>
      <user-name>Jaime Bellmyer</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Looks like a good well thought-out patch to me. Passes tests, seems like an elegant solution. +1 from me.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Looks like a good well thought-out patch to me. Passes tests,
seems like an elegant solution. +1 from me.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-02-26T23:37:35+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>[PATCH] Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-02-26T23:37:38+00:00</updated-at>
      <user-id type="integer">9830</user-id>
      <user-name>RSL</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>This is a nice enhancement that should let us avoid some horribly confusing errors, nice change.

However I'm wondering why you only did the check on insert_record.  Reading from the association would cause surprises too</body>
      <body-html>&lt;div&gt;&lt;p&gt;This is a nice enhancement that should let us avoid some
horribly confusing errors, nice change.&lt;/p&gt;
&lt;p&gt;However I'm wondering why you only did the check on
insert_record. Reading from the association would cause surprises
too&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-02T05:30:21+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>[PATCH] Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-02T05:30:23+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>Thanks for the feedback! @Michael: I picked the best single point of failure, in my opinion.  Creating is where the error occurs, and raising an exception on create means the database will never be corrupted in the first place.

Of course, I'm open to suggestions if you or anyone else really thinks this aspect should be changed.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Thanks for the feedback! @Michael: I picked the best single
point of failure, in my opinion. Creating is where the error
occurs, and raising an exception on create means the database will
never be corrupted in the first place.&lt;/p&gt;
&lt;p&gt;Of course, I'm open to suggestions if you or anyone else really
thinks this aspect should be changed.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-02T06:48:09+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>[PATCH] Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-02T06:48:12+00:00</updated-at>
      <user-id type="integer">47440</user-id>
      <user-name>Jaime Bellmyer</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>On insert seems good enough to me. How could you allready have data in it? If you do, then you have got more serious problems then this patch will solve for you.

Also if you did it differently, it would probably mean you will be validating all has_and_belongs_to_many relationships on every access, which would not be acceptable. +1 for this one.</body>
      <body-html>&lt;div&gt;&lt;p&gt;On insert seems good enough to me. How could you allready have
data in it? If you do, then you have got more serious problems then
this patch will solve for you.&lt;/p&gt;
&lt;p&gt;Also if you did it differently, it would probably mean you will
be validating all has_and_belongs_to_many relationships on every
access, which would not be acceptable. +1 for this one.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-03T23:29:07+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>[PATCH] Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-03T23:29:12+00:00</updated-at>
      <user-id type="integer">5639</user-id>
      <user-name>Bart ten Brinke</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer" nil="true"></assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>On insert seems good enough to me. How could you allready have data in it? If you do, then you have got more serious problems then this patch will solve for you.

Also if you did it differently, it would probably mean you will be validating all has_and_belongs_to_many relationships on every access, which would not be acceptable. +1 for this one.</body>
      <body-html>&lt;div&gt;&lt;p&gt;On insert seems good enough to me. How could you allready have
data in it? If you do, then you have got more serious problems then
this patch will solve for you.&lt;/p&gt;
&lt;p&gt;Also if you did it differently, it would probably mean you will
be validating all has_and_belongs_to_many relationships on every
access, which would not be acceptable. +1 for this one.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-03T23:29:28+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>[PATCH] Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-03T23:29:34+00:00</updated-at>
      <user-id type="integer">5639</user-id>
      <user-name>Bart ten Brinke</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name nil="true"></assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body></body>
      <body-html></body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-08T15:48:01+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- 
:assigned_user: 
:title: &quot;[PATCH] Primary key on HABTM join table now raises an exception&quot;
</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-08T15:48:07+00:00</updated-at>
      <user-id type="integer">1366</user-id>
      <user-name>Pratik</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>This patch doesn't seem to apply cleanly any more, but also it occurs to me that it could be worthwhile to cache the results of this test so we don't degrade performance for everyone's inserts?  

Or perhaps have this test only fire in development mode?  Or do all the adapters cache the results of the primary_key calls?</body>
      <body-html>&lt;div&gt;&lt;p&gt;This patch doesn't seem to apply cleanly any more, but also it
occurs to me that it could be worthwhile to cache the results of
this test so we don't degrade performance for everyone's
inserts?&lt;br&gt;&lt;/p&gt;
&lt;p&gt;Or perhaps have this test only fire in development mode? Or do
all the adapters cache the results of the primary_key calls?&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-09T07:49:00+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-09T07:49:04+00:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">1</attachments-count>
      <body>I've rebased, and attached the updated patch file, patch-2086b.diff.  I'll look into caching today, but I wanted to correct the conflict right away.</body>
      <body-html>&lt;div&gt;&lt;p&gt;I've rebased, and attached the updated patch file,
patch-2086b.diff. I'll look into caching today, but I wanted to
correct the conflict right away.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-09T15:39:29+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-09T15:39:32+00:00</updated-at>
      <user-id type="integer">47440</user-id>
      <user-name>Jaime Bellmyer</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">2</attachments-count>
      <body>The results of the primary key check are now cached, to save processing time on subsequent inserts.  I also added a test which verifies this behavior.  I moved the check itself out of insert_record, and into its own has_primary_key? instance method, with result caching:

@@@ ruby
# activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
def has_primary_key?
  return @has_primary_key unless @has_primary_key.nil?
  @has_primary_key = (ActiveRecord::Base.connection.supports_primary_key? &amp;&amp; 
    ActiveRecord::Base.connection.primary_key(@reflection.options[:join_table]))
end
@@@

The insert_record method now calls this new method:

@@@ ruby
# activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
def insert_record(record, force = true, validate = true)
  if has_primary_key?
    raise ActiveRecord::ConfigurationError, 
      &quot;Primary key is not allowed in a has_and_belongs_to_many join table (#{@reflection.options[:join_table]}).&quot;
  end

  # ...
end
@@@

Finally, the test (based on a join table that DOES contain a primary key):

@@@ ruby
# activerecord/test/cases/associations/habtm_join_table_test.rb
def test_should_cache_result_of_primary_key_check
  if ActiveRecord::Base.connection.supports_primary_key?
    ActiveRecord::Base.connection.stubs(:primary_key).with('my_books_my_readers').returns(false).once
    weaz = MyReader.create(:name=&gt;'Weaz')
      
    weaz.my_books &lt;&lt; MyBook.create(:name=&gt;'Great Expectations')
    weaz.my_books &lt;&lt; MyBook.create(:name=&gt;'Greater Expectations')
  end
end
@@@

The updated patch, patch-2086c.diff, has been attached.</body>
      <body-html>&lt;div&gt;&lt;p&gt;The results of the primary key check are now cached, to save
processing time on subsequent inserts. I also added a test which
verifies this behavior. I moved the check itself out of
insert_record, and into its own has_primary_key? instance method,
with result caching:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
def has_primary_key?
  return @has_primary_key unless @has_primary_key.nil?
  @has_primary_key = (ActiveRecord::Base.connection.supports_primary_key? &amp;amp;&amp;amp; 
    ActiveRecord::Base.connection.primary_key(@reflection.options[:join_table]))
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The insert_record method now calls this new method:&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# activerecord/lib/active_record/associations/has_and_belongs_to_many_association.rb
def insert_record(record, force = true, validate = true)
  if has_primary_key?
    raise ActiveRecord::ConfigurationError, 
      &amp;quot;Primary key is not allowed in a has_and_belongs_to_many join table (#{@reflection.options[:join_table]}).&amp;quot;
  end

  # ...
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Finally, the test (based on a join table that DOES contain a
primary key):&lt;/p&gt;


&lt;pre&gt;&lt;code class=&quot;ruby&quot;&gt;
# activerecord/test/cases/associations/habtm_join_table_test.rb
def test_should_cache_result_of_primary_key_check
  if ActiveRecord::Base.connection.supports_primary_key?
    ActiveRecord::Base.connection.stubs(:primary_key).with('my_books_my_readers').returns(false).once
    weaz = MyReader.create(:name=&amp;gt;'Weaz')
      
    weaz.my_books &amp;lt;&amp;lt; MyBook.create(:name=&amp;gt;'Great Expectations')
    weaz.my_books &amp;lt;&amp;lt; MyBook.create(:name=&amp;gt;'Greater Expectations')
  end
end
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The updated patch, patch-2086c.diff, has been attached.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-03-09T18:35:28+00:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-03-09T18:35:31+00:00</updated-at>
      <user-id type="integer">47440</user-id>
      <user-name>Jaime Bellmyer</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>Is there anything I can/should be doing to move this ticket forward?  I really think it's a valuable contribution, and I'd like to see it in Rails.</body>
      <body-html>&lt;div&gt;&lt;p&gt;Is there anything I can/should be doing to move this ticket
forward? I really think it's a valuable contribution, and I'd like
to see it in Rails.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-06-03T18:04:35+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">9903</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-06-03T18:04:40+01:00</updated-at>
      <user-id type="integer">47440</user-id>
      <user-name>Jaime Bellmyer</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.x</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body></body>
      <body-html></body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-06-10T04:41:03+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- 
:milestone: 9903
</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-06-10T04:41:03+01:00</updated-at>
      <user-id type="integer">141</user-id>
      <user-name>Michael Koziarski</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body></body>
      <body-html></body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-07T14:00:33+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- 
:tag: activerecord duplicate has_and_belongs_to_many join_table patch primary_key
</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bugmash duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-07T14:00:38+01:00</updated-at>
      <user-id type="integer">7211</user-id>
      <user-name>Mike Gunderloy</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>* verified
* still cleanly applies
* +1, i've seen this bite many people in the past...definitely prefer the more descriptive error message the patch gives us.
* tests look sufficient</body>
      <body-html>&lt;div&gt;&lt;ul&gt;
&lt;li&gt;verified&lt;/li&gt;
&lt;li&gt;still cleanly applies&lt;/li&gt;
&lt;li&gt;+1, i've seen this bite many people in the past...definitely
prefer the more descriptive error message the patch gives us.&lt;/li&gt;
&lt;li&gt;tests look sufficient&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-08T17:15:40+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bugmash duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-08T17:15:44+01:00</updated-at>
      <user-id type="integer">24346</user-id>
      <user-name>John Trupiano</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>+1 for the feature but the patch did not apply cleanly to 2-3-stable - I also attempted to apply it to master but no joy.

I was recently bitten by this last week, so it's great to see this patch! Please confirm that this should apply to 2-3-stable and I'll modify the patch to apply cleanly</body>
      <body-html>&lt;div&gt;&lt;p&gt;+1 for the feature but the patch did not apply cleanly to
2-3-stable - I also attempted to apply it to master but no joy.&lt;/p&gt;
&lt;p&gt;I was recently bitten by this last week, so it's great to see
this patch! Please confirm that this should apply to 2-3-stable and
I'll modify the patch to apply cleanly&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-08T19:57:10+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bugmash duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-08T19:57:12+01:00</updated-at>
      <user-id type="integer">22242</user-id>
      <user-name>Dan Pickett</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>+1 verified applies against 2-3-stable and master, tests look ok</body>
      <body-html>&lt;div&gt;&lt;p&gt;+1 verified applies against 2-3-stable and master, tests look
ok&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-08T20:23:02+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord bugmash duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-08T20:23:04+01:00</updated-at>
      <user-id type="integer">65576</user-id>
      <user-name>stevestmartin</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body></body>
      <body-html></body-html>
      <closed type="boolean">false</closed>
      <created-at type="datetime">2009-08-10T06:20:38+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- 
:tag: activerecord bugmash duplicate has_and_belongs_to_many join_table patch primary_key
</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>new</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-10T06:20:43+01:00</updated-at>
      <user-id type="integer">85</user-id>
      <user-name>Jeremy Kemper</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>(from [9c1bac0b7fcb627640db6824dca3e6e829a3c3e6]) raises an exception on habtm join table inserts if join table contains a primary key. Caches this check to save time on subsequent inserts.

[#2086 state:committed]

Signed-off-by: Jeremy Kemper &lt;jeremy@bitsweat.net&gt;
http://github.com/rails/rails/commit/9c1bac0b7fcb627640db6824dca3e6e829a3c3e6</body>
      <body-html>&lt;div&gt;&lt;p&gt;(from &lt;a href=
&quot;/projects/8994/changesets/9c1bac0b7fcb627640db6824dca3e6e829a3c3e6&quot;
title=
&quot;Changeset [9c1bac0b7fcb627640db6824dca3e6e829a3c3e6]&quot;&gt;[9c1bac0b7fcb627640db6824dca3e6e829a3c3e6]&lt;/a&gt;)
raises an exception on habtm join table inserts if join table
contains a primary key. Caches this check to save time on
subsequent inserts.&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;/projects/8994/tickets/2086&quot; title=
&quot;Ticket #2086&quot;&gt;#2086&lt;/a&gt; state:committed]&lt;/p&gt;
&lt;p&gt;Signed-off-by: Jeremy Kemper &lt;a href=
&quot;mailto:jeremy@bitsweat.net&quot;&gt;jeremy@bitsweat.net&lt;/a&gt;&lt;br&gt;
&lt;a href=
&quot;http://github.com/rails/rails/commit/9c1bac0b7fcb627640db6824dca3e6e829a3c3e6&quot;&gt;
http://github.com/rails/rails/commit/9c1bac0b7fcb627640db6824dca3e6...&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2009-08-10T06:20:53+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- 
:state: new
</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>committed</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-10T06:20:54+01:00</updated-at>
      <user-id type="integer">17393</user-id>
      <user-name>Repository</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>(from [9d51f6286680b832b0df5e3ce288575214c1de59]) raises an exception on habtm join table inserts if join table contains a primary key. Caches this check to save time on subsequent inserts.

[#2086 state:committed]

Signed-off-by: Jeremy Kemper &lt;jeremy@bitsweat.net&gt;
http://github.com/rails/rails/commit/9d51f6286680b832b0df5e3ce288575214c1de59</body>
      <body-html>&lt;div&gt;&lt;p&gt;(from &lt;a href=
&quot;/projects/8994/changesets/9d51f6286680b832b0df5e3ce288575214c1de59&quot;
title=
&quot;Changeset [9d51f6286680b832b0df5e3ce288575214c1de59]&quot;&gt;[9d51f6286680b832b0df5e3ce288575214c1de59]&lt;/a&gt;)
raises an exception on habtm join table inserts if join table
contains a primary key. Caches this check to save time on
subsequent inserts.&lt;/p&gt;
&lt;p&gt;[&lt;a href=&quot;/projects/8994/tickets/2086&quot; title=
&quot;Ticket #2086&quot;&gt;#2086&lt;/a&gt; state:committed]&lt;/p&gt;
&lt;p&gt;Signed-off-by: Jeremy Kemper &lt;a href=
&quot;mailto:jeremy@bitsweat.net&quot;&gt;jeremy@bitsweat.net&lt;/a&gt;&lt;br&gt;
&lt;a href=
&quot;http://github.com/rails/rails/commit/9d51f6286680b832b0df5e3ce288575214c1de59&quot;&gt;
http://github.com/rails/rails/commit/9d51f6286680b832b0df5e3ce28857...&lt;/a&gt;&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2009-08-10T06:20:54+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>committed</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-08-10T06:20:55+01:00</updated-at>
      <user-id type="integer">17393</user-id>
      <user-name>Repository</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>#3128 and #3190 are follow-ups on this feature.</body>
      <body-html>&lt;div&gt;&lt;p&gt;&lt;a href=&quot;/projects/8994/tickets/3128&quot; title=
&quot;Ticket #3128&quot;&gt;#3128&lt;/a&gt; and &lt;a href=&quot;/projects/8994/tickets/3190&quot;
title=&quot;Ticket #3190&quot;&gt;#3190&lt;/a&gt; are follow-ups on this feature.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2009-09-11T22:08:51+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>committed</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-09-11T22:08:54+01:00</updated-at>
      <user-id type="integer">85</user-id>
      <user-name>Jeremy Kemper</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
    <version type="Ticket::Version">
      <assigned-user-id type="integer">141</assigned-user-id>
      <attachments-count type="integer">3</attachments-count>
      <body>This patch just plain broke my app. Since AR doesn't support natively many-to-many double polymorphic relationships, we have a relationships table. Its migration is as follows:

class CreateRelationships &lt; ActiveRecord::Migration
  def self.up
    create_table :relationships, :id =&gt; false do |t|
      t.column :origin_id,        :integer, :null =&gt; false
      t.column :origin_type,      :string,  :null =&gt; false
      t.column :destination_id,   :integer, :null =&gt; false
      t.column :destination_type, :string,  :null =&gt; false
      t.column :position,         :integer, :null =&gt; true
    end
    
    add_index :relationships, [:origin_id, :origin_type, :destination_id, :destination_type], 
              :unique =&gt; true, 
              :name =&gt; 'index_relationship_polymorphs'
  end

  def self.down
    drop_table :relationships
  end
end

Do you have a solution for me? I need the primary key in this table.</body>
      <body-html>&lt;div&gt;&lt;p&gt;This patch just plain broke my app. Since AR doesn't support
natively many-to-many double polymorphic relationships, we have a
relationships table. Its migration is as follows:&lt;/p&gt;
&lt;p&gt;class CreateRelationships &amp;lt; ActiveRecord::Migration&lt;br&gt;
def self.up&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;create_table :relationships, :id =&amp;gt; false do |t|
  t.column :origin_id,        :integer, :null =&amp;gt; false
  t.column :origin_type,      :string,  :null =&amp;gt; false
  t.column :destination_id,   :integer, :null =&amp;gt; false
  t.column :destination_type, :string,  :null =&amp;gt; false
  t.column :position,         :integer, :null =&amp;gt; true
end

add_index :relationships, [:origin_id, :origin_type, :destination_id, :destination_type], 
          :unique =&amp;gt; true, 
          :name =&amp;gt; 'index_relationship_polymorphs'&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;end&lt;/p&gt;
&lt;p&gt;def self.down&lt;/p&gt;
&lt;pre&gt;
&lt;code&gt;drop_table :relationships&lt;/code&gt;
&lt;/pre&gt;
&lt;p&gt;end end&lt;/p&gt;
&lt;p&gt;Do you have a solution for me? I need the primary key in this
table.&lt;/p&gt;&lt;/div&gt;</body-html>
      <closed type="boolean">true</closed>
      <created-at type="datetime">2009-09-27T18:15:43+01:00</created-at>
      <creator-id type="integer">47440</creator-id>
      <diffable-attributes type="yaml">--- {}

</diffable-attributes>
      <milestone-id type="integer">41987</milestone-id>
      <number type="integer">2086</number>
      <permalink>patch-primary-key-on-habtm-join-table-now-raises-an-exception</permalink>
      <priority type="integer">0</priority>
      <project-id type="integer">8994</project-id>
      <state>committed</state>
      <tag>activerecord duplicate has_and_belongs_to_many join_table patch primary_key</tag>
      <title>Primary key on HABTM join table now raises an exception</title>
      <updated-at type="datetime">2009-09-27T18:15:48+01:00</updated-at>
      <user-id type="integer">71184</user-id>
      <user-name>tsenart</user-name>
      <creator-name>Jaime Bellmyer</creator-name>
      <assigned-user-name>Michael Koziarski</assigned-user-name>
      <url>http://rails.lighthouseapp.com/projects/8994/tickets/2086</url>
      <milestone-title>2.3.4</milestone-title>
    </version>
  </versions>
  <attachments type="array">
    <attachment type="Attachment">
      <code>83c17b74866365b198f9cee98320e4a7eddb1e1f</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-02-26T16:24:57+00:00</created-at>
      <filename>habtm_join_table_with_pk_raises_exception.diff</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">93396</id>
      <size type="integer">8032</size>
      <uploader-id type="integer">47440</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/93396/habtm_join_table_with_pk_raises_exception.diff</url>
    </attachment>
    <attachment type="Attachment">
      <code>0a6dd15df1b25e726cd81c66fa97106262ada8a7</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-03-09T15:39:29+00:00</created-at>
      <filename>patch-2086b.diff</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">97548</id>
      <size type="integer">8051</size>
      <uploader-id type="integer">47440</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/97548/patch-2086b.diff</url>
    </attachment>
    <attachment type="Attachment">
      <code>920605f120a5eddd1ced4218976973b9bff13038</code>
      <content-type>text/plain</content-type>
      <created-at type="datetime">2009-03-09T18:35:28+00:00</created-at>
      <filename>patch-2086c.diff</filename>
      <height type="integer" nil="true"></height>
      <id type="integer">97661</id>
      <size type="integer">11694</size>
      <uploader-id type="integer">47440</uploader-id>
      <width type="integer" nil="true"></width>
      <url>http://rails.lighthouseapp.com/attachments/97661/patch-2086c.diff</url>
    </attachment>
  </attachments>
</ticket>
