The keyword to make it work is detached
, so that we can pull from the NPC
type in general instead of the NPC
type we are inserting:
insert NPC {
name := 'I Love Mina',
lover := assert_single(
(select detached NPC filter .name like '%Mina%')
)
};
One other method that can work is this:
insert NPC {
name := 'I Love Mina',
lover := assert_single(
(select Person filter .name like '%Mina%')
)
};
We changed select NPC
to select Person
. Even though NPC
extends from Person
, Person
is a different type so we can draw from it without needing to write detached
.
2. How would you display up to 2 Person
types (and their name
property) whose names include the letter a
?
Like this, using like
and limit
:
select Person {
name
} filter .name like '%a%' limit 2;
Just use not exists
:
select Person {name} filter not exists .places_visited;
Take the original query:
select has_nine_in_it := <cal::local_time>'09:09:09';
and use <str>
to cast the cal::local_time
to a string, then add `like '%9%' at the end, like so:
select has_nine_in_it := <str><cal::local_time>'09:09:09' like '%9%';
First take the original insert:
insert NPC {
name := "The Innkeeper's Son",
age := 10
};
then wrap it in parentheses, add a select and remove the ;
select (
insert NPC {
name := "The Innkeeper's Son",
age := 10
}
);
Now just add the fields like in any other select
, then add age_ten_years_later
:
select (
insert NPC {
name := "The Innkeeper's Son",
age := 10
}
) {
name,
age,
age_ten_years_later := .age + 10
};
This gives us: {default::NPC {name: 'The Innkeeper\'s Son', age: 10, age_ten_years_later: 20}}