Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

AS in Dynamic Fields Selection Not Working #894

Open
chauhankiran opened this issue Jun 15, 2024 · 2 comments · May be fixed by #995
Open

AS in Dynamic Fields Selection Not Working #894

chauhankiran opened this issue Jun 15, 2024 · 2 comments · May be fixed by #995

Comments

@chauhankiran
Copy link

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.

@sumarlidason
Copy link

@chauhankiran did you find a workaround?

@chauhankiran
Copy link
Author

Not exactly per se. But, I've use .unsafe() as a workaround.

I've replaced columns array to string and then concatenating the columns instead of pushing in it.

let columns = "";

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

// ... other fields condition

if (view.field === "createdBy") {
  columns += 'creator.email as "creatorEmail",';
}

I'm using sql.unsafe() within SELECT.

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

@DictyosteliumDiscoideum DictyosteliumDiscoideum linked a pull request Dec 16, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants