Add ActiveBelongsToNotNull + nullable belongs_to detach#3113
Open
tyt2y3 wants to merge 1 commit into
Open
Conversation
af16e86 to
d0b0735
Compare
…elongs_to Delete now detaches
Introduce `ActiveBelongsToNotNull<E>` (variants: NotSet, Set) as the active-side
type for a non-nullable `belongs_to`. It has no `Delete` variant, so attempting
to detach a relation whose self-FK cannot be nulled is now a compile error rather
than a silent no-op or a raw SQL constraint error at runtime.
The derive macro inspects each `belongs_to` field's self-FK column:
- non-nullable FK -> field typed `ActiveBelongsToNotNull` (no delete_/set_option builders)
- nullable FK -> field typed `ActiveHasOne`, and `Delete` now detaches by
setting the self-FK to NULL (previously a no-op)
`from` is snake_cased before lookup so both a Column name (`BakeryId`) and a field
name (`user_id`) resolve to the FK field. Composite or unresolvable `from` values
fall back to `ActiveHasOne` with no detach, keeping them compiling.
Adds `From<HasOne>`/`From<HasMany>` conversions for the active types and a
`test_belongs_to_nullable_detach` integration test.
d0b0735 to
b15f647
Compare
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.
Follow-up to #3110.
Rename (
026dfe1e) — mechanical, word-boundary rename crate-wide:HasOneModel->ActiveHasOneHasManyModel->ActiveHasManyThese are the active/write-side companions to
HasOne/HasMany; the old*Modelnames read as loaded models. No behavior change.ActiveBelongsToNotNull+ nullable detach (af16e86f):ActiveBelongsToNotNull<E>with variantsNotSet/Set— noDelete. It is the active-side type for a non-nullablebelongs_to, so trying to detach a relation whose self-FK cannot be nulled is now a compile error instead of a silent no-op / raw SQL constraint error at runtime.belongs_to's self-FK column nullability:ActiveBelongsToNotNull(nodelete_*/set_*_optionbuilders generated)ActiveHasOne, andDeletenow detaches by setting the self-FK toNULL(previously a no-op)fromis snake_cased before lookup, so both a Column name (BakeryId) and a field name (user_id) resolve to the FK field. Composite / unresolvablefromvalues fall back toActiveHasOnewith no detach (keeps them compiling).Previously, calling delete/detach on a non-nullable
belongs_tosilently did nothing (or surfaced a raw DB constraint error). This makes the invariant type-level forbelongs_to— the only relation whose FK nullability is visible to the derive macro — while keeping codegen concrete per field so downstream users don't need generic bounds.