Skip to content

SQLite support for db_belongs_to and validates_db_uniqueness_of#77

Merged
bbatsov merged 8 commits intomasterfrom
pf-sqlite
Mar 20, 2026
Merged

SQLite support for db_belongs_to and validates_db_uniqueness_of#77
bbatsov merged 8 commits intomasterfrom
pf-sqlite

Conversation

@AlfonsoUceda
Copy link
Copy Markdown
Contributor

@AlfonsoUceda AlfonsoUceda commented Mar 20, 2026

SQLite support is extended to cover db_belongs_to validation and additional validates_db_uniqueness_of options (index_name, where, and complex/composite indexes).

Previously, SQLite's foreign_key_error_column returned a single column, which didn't work for disambiguating multiple db_belongs_to associations. The adapter now extracts all candidate columns from the INSERT SQL, and the Rescuer module iterates over them to find the correct validator — with a TOCTOU fallback for concurrent inserts.

The benchmark configurations are also updated to use DatabaseConfig (from config/database_config.rb) instead of hardcoded connection hashes, matching the convention in spec_helper.rb and Rakefile.

Adapter methods now receive the full error object instead of just
the message string. This allows the SQLite adapter to extract FK
column names from error.sql, since SQLite's error message lacks
column information.

For multi-FK models on SQLite, candidate columns are disambiguated
by querying which FK value is actually invalid, with a TOCTOU
fallback to the first matching validator.
…complex indexes

SQLite includes the index name in error messages for expression
indexes (format: UNIQUE constraint failed: index 'name'). Parsing
this enables index_name, where, complex index, and scoped index
support that was previously thought unsupported.
@AlfonsoUceda AlfonsoUceda changed the title sqlite [PF] SQLite support for db_belongs_to and validates_db_uniqueness_of Mar 20, 2026
@AlfonsoUceda AlfonsoUceda changed the title [PF] SQLite support for db_belongs_to and validates_db_uniqueness_of SQLite support for db_belongs_to and validates_db_uniqueness_of Mar 20, 2026
Each extracted method has a single responsibility: resolving
lookup keys from the DB error, and finding the best matching
validator with FK disambiguation and TOCTOU fallback.
@bbatsov bbatsov merged commit 77af497 into master Mar 20, 2026
5 checks passed
@AlfonsoUceda AlfonsoUceda deleted the pf-sqlite branch March 20, 2026 12:43
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants