Repository navigation
Conversation
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.
Description
Draft. 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, and the adapter returned that whole list as a single column name, so no validator matched and theActiveRecord::InvalidForeignKeywas raised instead.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.Why it matters: in
career-services-backend(CASE-4447), 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 became an exception.PostgresqlAdapter.foreign_key_error_columnsplits the key's columns.db_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.How to test
Locally on PostgreSQL 16:
spec/adapters/foreign_key_error_column_spec.rbandspec/validations/db_belongs_to_spec.rbpass for PostgreSQL (26 examples in thepostgresqlgroup, 0 failures) and for the adapter specs. The SQLite and MySQL groups could not run in this environment (no MySQL server, and the SQLite examples stop at setup withGem::LoadError: this environment's bundle has nosqlite3); CI covers them. RuboCop is clean on the changed lines (the one offense indb_belongs_to_spec.rb:145is already onmaster).🤖 Generated with Claude Code