Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

[Raku] Trying to get Red see the schema

by tomgracey (Beadle)
on Mar 26, 2021 at 22:25 UTC ( #11130406=perlquestion: print w/replies, xml ) Need Help??

tomgracey has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

So this is basic question about Raku. I am experimenting with it whilst having not the slightest clue what I'm doing. Just the module/package/class (which is it anyway?) loading system seems rather baffling. But I digress.

So here's the problem:

I have a basic schema file, which looks like this:

use Red; model User { has Int $!id is serial; has Str $.username is column is rw; has Str $.password is column is rw; has @.vmails is relationship(*.user_id, :model<Vmail>); } model Vmail { has Int $!id is serial; has Int $!user_id is referencing(*.id, :model<User>); has Str $.name is column; has $.user is relationship(*.id, :model<User>); }
And my script looks like this:
#!/usr/bin/raku use Red; use lib 'lib'; use Schema; my $*RED-DB = database "Pg", :user<vmail_user>, :dbname<vmail>, :passw +ord<--redacted-->; my $user = User.^create( username => 'Bill' ); print "*** DONE ***\n";
The database and tables exist in postgres (created manually rather than using Red) - and the table definitions are:
# \d user Table "public.user" Column | Type | Modifiers ----------+---------+------------------------------------------------- +-- id | integer | not null default nextval('user_id_seq'::regclass +) username | text | password | text | Indexes: "user_pkey" PRIMARY KEY, btree (id) Referenced by: TABLE "vmail" CONSTRAINT "vmail_user_id_fkey" FOREIGN KEY (user_id +) REFERENCES "user"(id) =# \d vmail Table "public.vmail" Column | Type | Modifiers ---------+---------+-------------------------------------------------- +-- id | integer | not null default nextval('vmail_id_seq'::regclass +) user_id | integer | name | text | Indexes: "vmail_pkey" PRIMARY KEY, btree (id) Foreign-key constraints: "vmail_user_id_fkey" FOREIGN KEY (user_id) REFERENCES "user"(id)
When I run the script I get the following output:
Could not find Vmail in: file#/var/www/apps/raku/vmail/lib inst#/root/.raku inst#/opt/rakudo-pkg/share/perl6/site inst#/opt/rakudo-pkg/share/perl6/vendor inst#/opt/rakudo-pkg/share/perl6/core ap# nqp# perl5# in block at /opt/rakudo-pkg/share/perl6/site/sources/9740DDE0E85E09 +3DCDF40F690C620BD9BAEE9078 (Red::Attr::Relationship) line 46 in method relationship-model at /opt/rakudo-pkg/share/perl6/site/sou +rces/9740DDE0E85E093DCDF40F690C620BD9BAEE9078 (Red::Attr::Relationshi +p) line 43 in method build-relationship at /opt/rakudo-pkg/share/perl6/site/sou +rces/9740DDE0E85E093DCDF40F690C620BD9BAEE9078 (Red::Attr::Relationshi +p) line 67 in method <anon> at /opt/rakudo-pkg/share/perl6/site/sources/C6C0BDE +B2D9A3E5141D8CD425806A5162AFE5DE9 (MetamodelX::Red::Relationship) lin +e 58 in method create at /opt/rakudo-pkg/share/perl6/site/sources/A963FC4 +C1C5D6923CAA0DC1451EE51EA254DA1ED (MetamodelX::Red::Model) line 483 in block <unit> at test_red.raku line 9

Hm... I don't think I am such a huge fan of the SHA-1 hashes...

Any idea why it can't find Vmail? I thought it might be related to the order of declaration of the model statements. I note that one of the examples (here: https://github.com/FCO/Red/blob/master/examples/alive/Schema.rakumod) seems to declare empty(?) models first and redeclare them later (to avoid referencing a model the that doesn't exist? Seems a bit ugly). I don't know if this has anything to do with it, but I tried adding model Vmail{...} at the top with no effect on the output

I only ever ask dumb questions and don't expect this to be any different...! But any help for a raku first-timer would be much appreciated.

Thanks!

Replies are listed 'Best First'.
Re: [Raku] Trying to get Red see the schema
by smokemachine (Hermit) on Mar 28, 2021 at 14:29 UTC
    Hi! Sorry, I was too late to answer that on #raku. The problem here is that when you create a `:model<Something>` Red tries to `use` "Something", on that case `Vmail` but that file does not exist, to fix that you could simply add `:require<Schema>` on that.
    use Red; model User is table<_user> { has Int $!id is serial; has Str $.username is column is rw; has Str $.password is column{ :nullable } is rw; has @.vmails is relationship(*.user_id, :model<Vmail>, :requir +e<Schema>); } model Vmail { has Int $!id is serial; has Int $!user_id is referencing(*.id, :model<User>, :require< +Schema>); has Str $.name is column; has $.user is relationship(*.id, :model<User>, :require<Schema +>); }
    I'm trying to make it not necessary.

      Ah ok I see - thank you that is very helpful!

      I notice that you call your table _user (ie with an underscore). Is this because user is a reserved word in Postgres? Is there a way to actually have a table called user? I mean, in Postgres you would just have to make sure to double quote the word. Do you know if there's a way to switch on double quoting of table names?

        I'm just finishing my last change to make it possible to use any table name. It's going to wrap the table name with "". I'll let you know when it's committed.
Re: [Raku] Trying to get Red see the schema
by smokemachine (Hermit) on Mar 28, 2021 at 14:48 UTC
    It seems https://github.com/FCO/Red/blob/master/examples/alive/Schema.rakumod has the same problem as your code. I'll fix that example (that was written before `:api<2>`). About the:
    model Log {...} model Person {...}
    That's the raku way to use a class/model before defining it, raku parses it in only 1 pass.
Re: [Raku] Trying to get Red see the schema
by erix (Prior) on Mar 26, 2021 at 23:00 UTC

    Could it be a Vmail / vmail confusion?

    (because postgres will make all table/columns names lowercase, unless they are double-quoted)

    (just a guess - I know nothing about Raku)

      Thanks for the suggestion. It could well be something like this. I am under the impression that when Red generates tables from schema files it converts names to lowercase, so I would assume it should pick up manually created ones just the same. Also from the output it does look like its missing a file rather than a db table. I could well be mistaken though!

Re: [Raku] Trying to get Red see the schema
by perlfan (Vicar) on Mar 27, 2021 at 02:29 UTC
    I only ever ask dumb questions and don't expect this to be any different...! But any help for a raku first-timer would be much appreciated.

    You'd probably find a lot more help on the perl6/raku mailing list. No doubt some monks here can be helpful; but just so you don't waste too much time this is not the best place for these questions.
      The question is only some hours old, and some monks are very much into Raku.

      So better don't call it off yet. ... :)

      > this is not the best place for these questions.

      I know you meant it in a helpful way. But saying something like this might set a tone. :)

      Raku questions are very much on topic and welcome here.

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery

        yes, trying to be helpful and minimize wasted time; it'd actually be nice if there was some Raku section, yanno, for good will; for most it just looks like a crusty old perl 5 help site :) - I should note OP's use of [Raku] was nice and shows awareness. Can't count on most to be so courteous I'd think.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://11130406]
Front-paged by haukex
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2021-04-15 12:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?