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

Wrong table alias in "whereColumn" clause with tables from separate PostgreSQL schemas #67

Open
romash1408 opened this issue Oct 3, 2022 · 1 comment

Comments

@romash1408
Copy link

Issue type:

[ ] Question
[x] Bug report
[ ] Feature request
[ ] Documentation issue

Database system/driver:

[x] Postgres
[ ] MSSQL
[ ] MySQL
[ ] MariaDB
[ ] SQLite3
[ ] Oracle
[ ] Amazon Redshift

typed-knex version:

4.13.1

Knex.js version:

0.19.5

Steps to reproduce or a small repository showing the problem:

import Knex from 'knex';
import { Column, Table, TypedKnex } from '@wwwouter/typed-knex';

@Table('schema1.table1')
class Table1 {
  @Column({ primary: true })
  id: number;
}

@Table('schema2.table2')
class Table2 {
  @Column({ primary: true })
  id: number;

  @Column({ name: 'table1_id' })
  linkedId: number;

  @Column({ name: 'table1_id' })
  linked: Table1;
}

const knex = Knex({
  client: 'pg',
  connection: { /** ... */ }
});

const typedKnex = new TypedKnex(knex)

const query = typedKnex.query(Table1)
  .whereExists(
    Table2,
    (subQuery, t1) => subQuery.whereColumn('linkedId', '=', 'id')
  )
  .select('id')
  .toQuery();

console.log(query);

output:

select "schema1"."table1"."id" as "id"
from "schema1"."table1"
where exists (
    select *
    from "schema2"."table2" as "subquery0$schema2.table2"
    where "subquery0$schema2"."table2"."table1_id" = "schema1"."table1"."id"
    --                       ^ this dot is wrong
)

expected

select "schema1"."table1"."id" as "id"
from "schema1"."table1"
where exists (
    select *
    from "schema2"."table2" as "subquery0$schema2.table2"
    where "subquery0$schema2.table2"."table1_id" = "schema1"."table1"."id"
)

or maybe remove dots from alias?

select "schema1"."table1"."id" as "id"
from "schema1"."table1"
where exists (
    select *
    from "schema2"."table2" as "subquery0$schema2$table2"
    where "subquery0$schema2$table2"."table1_id" = "schema1"."table1"."id"
)
@wwwouter
Copy link
Owner

wwwouter commented Oct 8, 2022

Hi, I've been thinking about this for a bit and would it be okay to specify the schema separately?
Something like this:

@Table({name:'table1', schema:'schema1'})
class Table1 {
  @Column({ primary: true })
  id: number;
}

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

No branches or pull requests

2 participants