Repository navigation
[CASE-4447] Handle a violated composite foreign key in db_belongs_to on PostgreSQL - #83
Merged
Merged
Conversation
aitorlb
marked this pull request as ready for review
October 27, 2026 09:38
AlfonsoUceda
approved these changes
Oct 27, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Ticket: CASE-4447
Draft. Related: toptal/career-services-backend#2195 adds the composite keys that hit this, and merges only after this fix is released.
Problem
On PostgreSQL,
db_belongs_toturns a foreign-key violation into the association'smust existerror only when the key has one column. A composite key reports every column,Key (tenant_id, company_id)=(2, 1) is not present, andPostgresqlAdapter.foreign_key_error_columnreturned that whole list as a single column name, so no validator matched and theActiveRecord::InvalidForeignKeywas raised instead.In
career-services-backend(CASE-4447, toptal/career-services-backend#2195), every foreign key between two brand-owned tables gets a composite partner over(brand_id, <column>)next to the single-column key. Postgres checks a row's keys in the order of their internal trigger names, so for a parent id that does not exist either key can report it; when it is the composite one, amust existvalidation failure becomes an exception.Fix
The adapter now returns each column as a candidate, as the SQLite adapter already does for a multi-column insert. The rescuer picks the candidate with a
db_belongs_to, and when there are several it already checks which referenced row is missing (foreign_key_invalid?), so nothing else changes.Tests
spec/adapters/foreign_key_error_column_spec.rb: the adapter extracts every column of a composite key.spec/validations/db_belongs_to_spec.rb: on PostgreSQL, adb_belongs_to :companyrecord pointing at a company of another tenant (single-column key satisfied, composite key violated) getserrors[:company]instead of an exception. Without the fix, that example raisesActiveRecord::InvalidForeignKey.Locally on PostgreSQL 16 both specs pass (26 examples in the
postgresqlgroup, 0 failures, plus the adapter specs). RuboCop is clean on the changed lines (the one offense indb_belongs_to_spec.rb:145is already onmaster).Not run
MySQL and SQLite: there was no MySQL server, and the SQLite examples stop at setup with
Gem::LoadErrorbecause this environment's bundle has nosqlite3. The change touches only the PostgreSQL adapter; CI covers the others.🤖 Generated with Claude Code