Skip to content

fix(db): classify foreign key violations - #644

Merged
m4tx merged 2 commits into
cot-rs:masterfrom
oxura:fix/541-foreign-key-error
Aug 29, 2026
Merged

m4tx merged 2 commits into
cot-rs:masterfrom
oxura:fix/541-foreign-key-error

Conversation

@oxura

@oxura oxura commented Aug 29, 2026

Copy link
Copy Markdown
Contributor

Fixes #541

Description

map_sqlx_error normalized unique constraint failures but left foreign key failures as backend errors. Saving a model with a missing referenced primary key therefore produced the wrong public error.

This adds DatabaseError::ForeignKeyViolation, maps SQLx's foreign-key classification to it, and covers the missing-parent insert through the model API.

Type of change

  • Bug fix
  • New feature
  • Documentation
  • Refactor / cleanup
  • Performance improvement
  • Other (describe above)

Validation

  • cargo test -p cot --all-features --test db db_testing::relations::foreign_keys_sqlite -- --exact
  • cargo test -p cot --all-features --test db (55 passed, 104 external-database tests ignored)
  • cargo clippy --no-deps --all-targets -- -D warnings
  • cargo fmt --all --check
  • git diff --check upstream/master...HEAD

I did not run the ignored PostgreSQL/MySQL tests locally; those require the Docker Compose services and remain covered by CI.

Checklist

  • I've read the contributing guide
  • Tests pass locally (just test-all)
  • Code passes clippy (just clippy)
  • Code is properly formatted (cargo fmt)
  • New tests added (regression test for bugs, coverage for new features)
  • Documentation (both code and site) updated (if applicable)

@github-actions github-actions Bot added the C-lib Crate: cot (main library crate) label Aug 29, 2026

@seqre seqre left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Welcome to Cot and thank you for your contribution! One small fix and it should be okay to merge

Comment thread cot/tests/db_testing/relations.rs Outdated

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It needs fixing with the addition of the new error, probably just this should be enough, but please test to ensure it works!

Suggested change
assert!(matches!(error, DatabaseError::ForeignKeyViolation));

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Updated. The delete-path assertion now expects ForeignKeyViolation. SQLite reports ON DELETE RESTRICT as SQLITE_CONSTRAINT_TRIGGER (1811), rather than its usual foreign-key code, so I extended the mapper for that exact code/message pair. The focused test and the full cot database test target pass.

@m4tx m4tx left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me, thanks!

@m4tx
m4tx enabled auto-merge (squash) August 29, 2026 11:12
@m4tx
m4tx merged commit ebfb0af into cot-rs:master Aug 29, 2026
23 of 24 checks passed
@cotbot cotbot Bot mentioned this pull request Aug 25, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

C-lib Crate: cot (main library crate)

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Show better error message when creating ForeignKey::PrimaryKey with non-existent ID

3 participants