question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Subquery with date: No overload matches this call

See original GitHub issue

Hi, I need help once more… I’m trying to create a subquery with a datetime column.

This is the table definition:

export const tMessage = new class TMessage extends Table<DBConnection, 'TMessage'> {
    public uid = this.autogeneratedPrimaryKey<u64>('uid', 'custom', CUSTOM_TYPES.UID_MESSAGE);
    public processedAt = this.column('processedAt', 'localDateTime');
}

I want to select all messages, where the processedAt is smaller or equal to the processedAt of a message with a certain uid.

this._db
    .selectFrom(tMessage)
    .select({
        uid: tMessage.uid,
        processedAt: tMessage.processedAt,
    })
    .where(
        tMessage.processedAt.lessOrEquals(
            // Subquery
            this._db
                .selectFrom(tMessage)
                .where(tMessage.uid.equals(referenceUid))
                .selectOneColumn(tMessage.processedAt),
        ),
    )
    .orderBy('processedAt', 'asc')
    .limit(limit)
    .executeSelectMany()

However, this fails:

No overload matches this call.
...
Argument of type 'GroupByOrderByExecutableSelectExpression<DB<"DBConnection">, TMessage, undefined, Date, NoTableOrViewRequiredView<DB<"DBConnection">>, "result">' is not assignable to parameter of type 'Date'.
Argument of type 'GroupByOrderByExecutableSelectExpression<DB<"DBConnection">, TMessage, undefined, Date, NoTableOrViewRequiredView<DB<"DBConnection">>, "result">' is not assignable to parameter of type 'IComparableValueSource<TableOrViewRef<DB<"DBConnection">>, Date>'.
Argument of type 'GroupByOrderByExecutableSelectExpression<DB<"DBConnection">, TMessage, undefined, Date, NoTableOrViewRequiredView<DB<"DBConnection">>, "result">' is not assignable to parameter of type 'IComparableValueSource<TableOrViewRef<DB<"DBConnection">>, Date | null | undefined>'.

Is this because the subquery may return undefined, and undefined cannot be compared to a Date column?

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Comments:6 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
juanluispazcommented, Oct 8, 2021

I’m making this test:

create table test(id numeber, value number);

insert into test values(1, 10);
insert into test values(2, 20);
insert into test values(3, 30);
insert into test values(4, 40);
insert into test values(5, 50);

select * from test where value < (select value from test where id = 3);

It works on Sqlite, I’m so surprised, I didn’t expect it 😄; that is not clearly documented in the sqlite documentation. I’m going to continue investigating if all other databases have the same feature. For sure this case must be in the library.

Regarding the case you describes, you can use a left join.

If you want to get all messages only if the referenceUid exists the previous query is valid for you (I updated because I used the wrong join):

const reference = tMessage.as('reference')

this._db
    .selectFrom(tMessage)
    .join(reference).on(reference.uid.equals(referenceUid))
    .select({
        uid: tMessage.uid,
        processedAt: tMessage.processedAt,
    })
    .where(
        tMessage.processedAt.lessOrEquals(reference.processedAt)
    )
    .orderBy('processedAt', 'asc')
    .limit(limit)
    .executeSelectMany()

But if you want to get all the messages in the case of the referenceUid doesn’t exists this can works for you:

const reference = tMessage.forUseInLeftJoinAs('reference')

this._db
    .selectFrom(tMessage)
    .leftJoin(reference).on(reference.uid.equals(referenceUid))
    .select({
        uid: tMessage.uid,
        processedAt: tMessage.processedAt,
    })
    .where(
        tMessage.processedAt.lessOrEquals(reference.processedAt)
    ).or(
        reference.processedAt.isNull()
    )
    .orderBy('processedAt', 'asc')
    .limit(limit)
    .executeSelectMany()
0reactions
threema-danilocommented, Oct 15, 2021

Great, thank you @juanluispaz!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Error No overload matches this call TS2769 - Stack Overflow
Depends on what you want date to be if res.date is undefined. A possibility might be date: res && res.date && new Date(res.date)....
Read more >
4 beta, useQuery No overload matches this call. #3502 - GitHub
Describe the bug Type error: No overload matches this call. Overload 1 of 3, '(queryKey: QueryKey, options?
Read more >
CALL {} (subquery) - Cypher Manual - Neo4j
The `+CALL {}+` clause evaluates a subquery that returns some values. ... a RETURN statement are called returning subqueries while subqueries without such...
Read more >
No overload matches this call error in TypeScript | bobbyhadz
The error "No overload matches this call" occurs when we call a function and pass it a parameter that doesn't match any of...
Read more >
Scalar Expressions | CockroachDB Docs
In most cases, conditions and result expressions after the first match are not evaluated. The exception is subqueries, which are eagerly evaluated when ......
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found