feat: .^create on belongs-to relationship with auto FK#596
Conversation
Port from PR FCO#523 (2021): 1. Fix ast-value .ref in relationship-ast — string model references now work correctly in relationship conditions. (Red::AST::Eq.new: $\_, ast-value .ref: $t2) 2. Add parent/join-on to alias role — enables tracking the parent object in relationships, needed for auto FK on create. 3. Enhance .^create to handle relationship accessors: when called via $ble.bla.^create(...), automatically sets the foreign key on the parent object and saves it. 4. Tests: belongs-to create with auto FK, isolation between instances. Closes FCO#523
- String model references (:model<Name>) - Type model references (:model(Type)) - has-one create via relationship accessor - Multiple creates on same parent - Create with FK already set - Isolation between parent instances - Extra attributes passthrough - Unsaved parent error case - FK default=0 edge case - To-many .create regression checks
| #| And Lists and/or Hashes for relationships | ||
| multi method create(\model where *.DEFINITE, *%orig-pars, :$with where not .defined) is hidden-from-backtrace is rw { | ||
| die "Cannot call .^create on a defined model." if model.DEFINITE; | ||
| multi method create(\\mo where *.DEFINITE, *%orig-pars, :$with where not .defined) is hidden-from-backtrace is rw { |
There was a problem hiding this comment.
Verdade! Era escaping bug — o arquivo ficou com 4 barras em vez de 1. Corrigido em 2ff9196.
| multi method create(\model where *.DEFINITE, *%orig-pars, :$with where not .defined) is hidden-from-backtrace is rw { | ||
| die "Cannot call .^create on a defined model." if model.DEFINITE; | ||
| multi method create(\\mo where *.DEFINITE, *%orig-pars, :$with where not .defined) is hidden-from-backtrace is rw { | ||
| my \\model = mo.^orig; |
There was a problem hiding this comment.
Mesmo problema de escaping. Corrigido junto.
…e expected)
- t/35-create.rakutest: :default(0) → :default{ 0 } fixes 'Type check failed in assignment to &!default'
- lib/Red/Column.rakumod: .perl() → .raku()
- lib/Red/Model.rakumod: .get_value(self).perl → .get_value(self).raku
- lib/X/Red/Exceptions.rakumod: $.orig-exception.perl → $.orig-exception.raku
- lib/Red/Driver/Mock.rakumod: $re.perl() → $re.raku()
- lib/Red/Cli.rakumod: %pars.map(*.perl) → %pars.map(*.raku)
- lib/Red/Driver.rakumod: @bind.perl()/@binds.perl() → @bind.raku()/@binds.raku()
Code Review — feat: .^create on belongs-to relationship with auto FKVerdict: Comment (0 critical, 2 warnings, 4 suggestions) Port and fix of PR #523 (from 2021). Enables ✅ Looks Good
|
| Metric | Value |
|---|---|
| Files changed | 9 |
| Lines added | +216 |
| Lines removed | −18 |
| New tests | t/35-create.rakutest (10 new subtests) |
| Security scan | Clean |
Reviewed by Hermes Agent
What
Port and fix of PR #523 (from 2021).
Enables calling
.^createdirectly on a belongs-to relationship accessor, with automatic foreign key management:Changes
1. Fix:
ast-value .refin relationship-astString model references now work correctly (
Red::AST::Eq.new: $_, ast-value .ref: $t2)2. Add
parent/join-onto alias roleEnables tracking the parent object for FK auto-set during create.
3. Enhance
.^createmetamodel methodWhen invoked on a relationship alias (has
join-on+parent), creates the related object and automatically sets the FK on the parent, then saves it.4. Tests
Files
lib/MetamodelX/Red/Model.rakumod— create method + alias rolelib/Red/Attr/Relationship.rakumod— ast-value fixt/35-create.rakutest— new testsCloses #523