Skip to content

Revisit the ordering of toSQL being called vs the fragment embedding #2854

@hos

Description

@hos

Summary

This plugin generates two different SQLs one for parent (chat) and one for the "profiles" in which case the SQL of the "profiles" can't access the alias "chat" as it's executed in a different query. Wrapping the "$chatId" inside $profiles.placeholder($chatId, TYPES.bigint) solves the issue by passing "chatId" as param.

const $profiles = profiles.find();
const $chatId = $chat.get('chat_id');

$profiles.where(
  sql.fragment`${$profiles.alias}.user_id in (
    select user_id from public.participants where chat_id = ${$chatId}
  )`
);

return $profiles;

Expected results

It would be better to inline this query in the original SQL query instead of executing new query.

Actual results

Reproducible

benjie/ouch#40

The plan generates a different SQL query.

Additional context

From the conversation of the discord chat the ordering of calling toSQL being called vs the fragment embedding can be reconsidered to solve this issue and allow to use the $chatId without having to wrap it into "placeholder".

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    Status

    🌳 Triage

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions