typed-knex
typed-knex copied to clipboard
Wrong table alias in "whereColumn" clause with tables from separate PostgreSQL schemas
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"
)
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;
}