Subquery from a function
I want to reuse a sub-query that refers to one of the fields in the root query's result.
I tried doing this:
However, I get a bunch o' errors I don't quite understand:
If I pick that exact expression and paste it directly in the
I assume that it's trying to tell me that
I tried doing this:
However, I get a bunch o' errors I don't quite understand:
If I pick that exact expression and paste it directly in the
.select(eb => [/* here */]) of the parent query, then there are no errors.I assume that it's trying to tell me that
check_is_friend might collide with the parent's scope or something like that? Am I doing something silly, is there a better way to do this?Solution
Don't pass in the expression builder. Its type easily becomes incompatible in other contexts.
Don't use an explicit result type. The result type here is NOT
You always need to provide a name for selections using the
and then use it like this
This is definitely not true. Your code had a bunch of errors. If that worked, you've disabled typescript type-checks completely.
Yes, the compiled javascript probably works by accident. But you've compiled it without any type-checks if it does.
Don't use an explicit result type. The result type here is NOT
boolean. It's { is_friend: boolean }. Yes, you can use that as a scalar in SQL and Kysely does handle that correctly. But don't explicitly set the wrong type.You always need to provide a name for selections using the
as method. The name is dialect-specific if you leave it out. Since kysely types don't know which dialect you're using, providing a name for that column automatically is impossible.and then use it like this
If I pick that exact expression and paste it directly in the .select(eb => [/* here */]) of the parent query, then there are no errors.
This is definitely not true. Your code had a bunch of errors. If that worked, you've disabled typescript type-checks completely.
Yes, the compiled javascript probably works by accident. But you've compiled it without any type-checks if it does.