porsager / porsager/postgres

AS in Dynamic Fields Selection Not Working

Open
#894 2 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
JavaScript
Stars
8.7k
Forks
374
Avg merge
11d 16h
Merged PRs (30d)
1

Description

I've a columns variable in which I'm conditionally adding column name based on whether field is enable or not. There is one field createdBy which I want to select as creator.email AS creatorEmail where creator is an alias of the users table. Following is the sample code.

let columns = [];

if (view.field === "id") {
  columns.push("companies.id");
}

// ... other fields condition

if (view.field === "createdBy") {
  columns.push("creator.email AS creatorEmail");
}

Then following the query I'm trying to run.

sql`
  select
    ${sql(columns)}
  from
    companies
  join
    users as creator
  on
    companies."createdBy" = creator.id`

But, it throw error as

PostgresError: column creator.email AS creatorEmail does not exist

Postgres condering complete string as column name and not As as keyword of Postgres.

Note: I saw one issue where someone suggested to use fragment. But, can I've an example of it? I tried with many different syntax and it doesn't work.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start at the sql tagged-template interpolation shown in the issue, especially sql(columns), and compare it with the fragment usage the issue requests. Done means establishing a supported way to compose the aliased select expression without treating the whole string as an identifier, with an example or test covering it.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, postgresql
Domain
databases
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
38/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.