Skip to content

WiP: .^create from belongs-to relationship#523

Open
FCO wants to merge 5 commits into
masterfrom
create-to-one
Open

WiP: .^create from belongs-to relationship#523
FCO wants to merge 5 commits into
masterfrom
create-to-one

Conversation

@FCO

@FCO FCO commented Nov 6, 2021

Copy link
Copy Markdown
Owner

This is not working when using strings as referencing's model (but works if use the actual type)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Pull request overview

This PR attempts to make .^create work when invoked from a belongs-to (single) relationship where the referenced model is specified by string (e.g. :model<Bli>), by ensuring the relationship context (alias/join info + parent) is preserved so the FK can be propagated back to the owning row.

Changes:

  • Adds new test coverage for creating records via single/belongs-to relationships (including type-based relationship declarations).
  • Adjusts relationship/alias machinery so relationship selectors can carry a parent reference and accept an explicit alias in join conditions.
  • Updates relationship AST generation and reference resolution to better handle aliasing.

Reviewed changes

Copilot reviewed 5 out of 6 changed files in this pull request and generated 6 comments.

Show a summary per file
File Description
t/35-create.t Adds new subtests for create-from-relationship scenarios; reorders an existing array-create subtest.
lib/Red/PrepareCode.pm6 Minor formatting/line-number-only change.
lib/Red/Column.pm6 Tweaks ReferencesProxy.CALL-ME to treat definite aliases differently when resolving references.
lib/Red/Attr/Relationship.pm6 Adjusts relationship AST generation to wrap RHS with ast-value.
lib/MetamodelX/Red/Relationship.pm6 Changes scalar relationship accessor behavior to try to return a join alias with parent set.
lib/MetamodelX/Red/Model.pm6 Modifies alias role to add parent and parameterize join-on with an alias argument.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment thread t/35-create.t
schema(Bla, Ble).drop.create;

# TODO: Figure out why this test can't be the last one
subtest "Create on has-one", {
Comment thread t/35-create.t
Comment on lines +32 to +33
is $bla.bles.head.gist, $ble.gist;
is $ble.bla.gist, $bla.gist;
Comment thread t/35-create.t
is $ble.bla.gist, $bla.gist;
}

subtest "belogs-to using types", {
Comment thread t/35-create.t
Comment on lines +56 to +57
is $bli.blos.head.gist, $blo.gist;
is $blo.bli.gist, $bli.gist;
Comment on lines 225 to +227
my role RAlias[Red::Model:U \rtype, Str $rname, \alias, \rel, \base, \join-type, @cols] {
method columns(|) { @cols }
method table(|) { rtype.^table }
method as(|) { self.table-formatter: $rname }
method orig(|) { rtype }
method join-type(|) { join-type }
method tables(|) { [ |base.^tables, alias ] }
method join-on(|) {
method parent(|) is rw { $ }
method columns(|) { @cols }
Comment on lines +163 to +167
my \value = nqp::getattr(self, self.WHAT, $attr.name);
my $n = $self.WHAT."$name"();
$n.^parent = self if $n.HOW.^can: "parent";
return $n unless value;
value
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