<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>Forem: tomdonarski</title>
    <description>The latest articles on Forem by tomdonarski (@tom_donarski_dev).</description>
    <link>https://forem.com/tom_donarski_dev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3692289%2Fe0708bdf-4fb4-43c2-91c3-ba1a68fdc183.jpg</url>
      <title>Forem: tomdonarski</title>
      <link>https://forem.com/tom_donarski_dev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://forem.com/feed/tom_donarski_dev"/>
    <language>en</language>
    <item>
      <title>Wait, what? PK is not needed for HABTM?</title>
      <dc:creator>tomdonarski</dc:creator>
      <pubDate>Sun, 04 Jan 2026 08:16:28 +0000</pubDate>
      <link>https://forem.com/tom_donarski_dev/wait-what-pk-is-not-needed-for-habtm-25lb</link>
      <guid>https://forem.com/tom_donarski_dev/wait-what-pk-is-not-needed-for-habtm-25lb</guid>
      <description>&lt;p&gt;While skimming over &lt;a href="https://guides.rubyonrails.org/active_record_composite_primary_keys.html" rel="noopener noreferrer"&gt;Rails’ docs&lt;/a&gt; I’ve found a sentence about HABTM (&lt;code&gt;has_and_belongs_to_many&lt;/code&gt;) association that tickled my interest:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;(…) For &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; associations, you'll need to create a join table &lt;strong&gt;without&lt;/strong&gt; a primary key.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;It made my mind go blank for a moment, and I started to wonder "Hey, can a table do not have a primary key?"&lt;/p&gt;

&lt;p&gt;👉 Yes, it's possible. Primary key is just a constraint (&lt;a href="https://www.postgresql.org/docs/current/sql-createtable.html" rel="noopener noreferrer"&gt;source&lt;/a&gt;).&lt;/p&gt;

&lt;p&gt;Then I went on to inspect an immediate project to see if there’s any join table for HABTM association, and saw this:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;CREATE TABLE public.organizations_vendors (
    id integer NOT NULL,
    organization_id integer,
    vendor_id integer
);
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I started digging, and found that the HABTM join table is an &lt;strong&gt;implementation detail&lt;/strong&gt;, not a real model. Which is also hinted by &lt;a href="https://guides.rubyonrails.org/association_basics.html#has-many-through-vs-has-and-belongs-to-many" rel="noopener noreferrer"&gt;Rails’ docs&lt;/a&gt;:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You'd use &lt;code&gt;has_and_belongs_to_many&lt;/code&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The association is simple and does not require additional attributes or behaviors on the join table.&lt;/li&gt;
&lt;li&gt;You do not need validations, callbacks, or extra methods on the join table.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

&lt;p&gt;It turns out PK on a HABTM join table is not required, as functionally it’s irrelevant.&lt;/p&gt;

&lt;p&gt;If it is (application-wise) - then most likely &lt;code&gt;has_many :through&lt;/code&gt; would be a better choice (&lt;a href="https://guides.rubyonrails.org/association_basics.html#has-many-through-vs-has-and-belongs-to-many" rel="noopener noreferrer"&gt;source&lt;/a&gt;).&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;You'd use &lt;code&gt;has_many :through&lt;/code&gt; when:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;You need to add extra attributes or methods to the join table.&lt;/li&gt;
&lt;li&gt;You require validations or callbacks on the join model.&lt;/li&gt;
&lt;li&gt;The join table should be treated as an independent entity with its own behavior.&lt;/li&gt;
&lt;/ul&gt;
&lt;/blockquote&gt;

</description>
      <category>database</category>
      <category>rails</category>
      <category>ruby</category>
      <category>sql</category>
    </item>
  </channel>
</rss>
