FCO / Red

A WiP ORM for Raku
Artistic License 2.0
70 stars 27 forks source link

Red operators leak Red context #539

Closed patrickbkr closed 2 years ago

patrickbkr commented 2 years ago

The following code should print Hi 1 but instead prints

Red::AST::Concat:
    Hi 
    (citest_set.id)::str
use Red:api<2>;

enum CITestSetStatus < NEW DONE >;
model CITestSet { ... }

model CIPlatformTestSet is rw is table<ciplatform_test_set> {
    has UInt      $.id          is serial;
    has UInt      $!fk-test-set is referencing( *.id, :model(CITestSet) );
    has CITestSet $.test-set    is relationship( *.fk-test-set );
}
model CITestSet is rw is table<citest_set> {
    has UInt              $.id                 is serial;
    has CITestSetStatus   $.status             is column = NEW;
    has CIPlatformTestSet @.platform-test-sets is relationship( *.fk-test-set );
}

red-defaults('SQLite');
schema(CIPlatformTestSet, CITestSet).create;

my $ts1 = CITestSet.^create;
my $pts = CIPlatformTestSet.^create: :$ts1;
my $ts2 = $pts.test-set;
say "Hi " ~ $ts2.id;
FCO commented 2 years ago

Thanks for the issue. I'll take a look asap. Do you know if $ts2 was defined on your code? The problem here seems to me to be the ^create not recognising :ts1 as a valid named parameter (there is no attribute called ts1) so (just as new) it ignores that parameter. Than it creates a obj without relationship. Then when you try to access $pts.test-set it returns a CITestSet type object and accessing any column on a model type object returns it's column attribute representation and then it uses Red's version of ~.

For fixing that, maybe it's just the case of changing :$ts1 for :test-set($ts1). Or even better: change both .^creates with a single:

$pts = CIPlatformTestSet.^create: :test-set{};

That should create CIAtformTestSet and the CITestSet related to it.

Please let me know if any of this helps.

FCO commented 2 years ago

Changing ti use the right named arg:

image
patrickbkr commented 2 years ago

Ouch. That's me failing to golf my codebase. :facepalm: Thank's for taking a look at this!

I think this is solved. Closing.

FCO commented 2 years ago

Good it's working! Have you seen my new branch? Do you think that would make it easier to spot that kind of problems?

patrickbkr commented 2 years ago

I have seen the branch. I think erroring on accessing columns on typeobjects will definitely make it easier to understand what's wrong in such cases. So a big +1 from me.