define where query outside

I dont understand why this code works :
const whereQuery = exists(
db
.select({ id: sql`1` })
.from(productsOrderTable)
.where(
and(
eq(productsOrderTable.orderId, ordersTable.id),
ilike(productsOrderTable.name, "%" + query + "%"),
),
),
)

const [orders, ordersLength] = await Promise.all([
db.query.ordersTable.findMany({
limit: ordersByPage,
offset: (page - 1) * ordersByPage,
with: {
products: true,
user: {
columns: {
password: false,
},
},
},
where: (ordersTable) => exists(
db
.select({ id: sql`1` })
.from(productsOrderTable)
.where(
and(
eq(productsOrderTable.orderId, ordersTable.id),
ilike(productsOrderTable.name, "%" + query + "%"),
),
),
), ### only changed this part of the code
orderBy: (ordersTable, { desc }) => desc(ordersTable.date),
}),
db
.select({ count: count() })
.from(ordersTable)
.where(whereQuery),
]);
const whereQuery = exists(
db
.select({ id: sql`1` })
.from(productsOrderTable)
.where(
and(
eq(productsOrderTable.orderId, ordersTable.id),
ilike(productsOrderTable.name, "%" + query + "%"),
),
),
)

const [orders, ordersLength] = await Promise.all([
db.query.ordersTable.findMany({
limit: ordersByPage,
offset: (page - 1) * ordersByPage,
with: {
products: true,
user: {
columns: {
password: false,
},
},
},
where: (ordersTable) => exists(
db
.select({ id: sql`1` })
.from(productsOrderTable)
.where(
and(
eq(productsOrderTable.orderId, ordersTable.id),
ilike(productsOrderTable.name, "%" + query + "%"),
),
),
), ### only changed this part of the code
orderBy: (ordersTable, { desc }) => desc(ordersTable.date),
}),
db
.select({ count: count() })
.from(ordersTable)
.where(whereQuery),
]);
But not this one :
const whereQuery = exists(
db
.select({ id: sql`1` })
.from(productsOrderTable)
.where(
and(
eq(productsOrderTable.orderId, ordersTable.id),
ilike(productsOrderTable.name, "%" + query + "%"),
),
),
)

const [orders, ordersLength] = await Promise.all([
db.query.ordersTable.findMany({
limit: ordersByPage,
offset: (page - 1) * ordersByPage,
with: {
products: true,
user: {
columns: {
password: false,
},
},
},
where: whereQuery, ### only changed this part of the code
orderBy: (ordersTable, { desc }) => desc(ordersTable.date),
}),
db
.select({ count: count() })
.from(ordersTable)
.where(whereQuery),
]);
const whereQuery = exists(
db
.select({ id: sql`1` })
.from(productsOrderTable)
.where(
and(
eq(productsOrderTable.orderId, ordersTable.id),
ilike(productsOrderTable.name, "%" + query + "%"),
),
),
)

const [orders, ordersLength] = await Promise.all([
db.query.ordersTable.findMany({
limit: ordersByPage,
offset: (page - 1) * ordersByPage,
with: {
products: true,
user: {
columns: {
password: false,
},
},
},
where: whereQuery, ### only changed this part of the code
orderBy: (ordersTable, { desc }) => desc(ordersTable.date),
}),
db
.select({ count: count() })
.from(ordersTable)
.where(whereQuery),
]);
1 Reply
Dr Use
Dr UseOP4w ago
I have this error
PostgresError: invalid reference to FROM-clause entry for table "orders"
at ErrorResponse
at handle
at Socket.data
at Socket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:376:12)
at readableAddChunk (node:internal/streams/readable:349:9)
at Readable.push (node:internal/streams/readable:286:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '42P01',
hint: 'Perhaps you meant to reference the table alias "ordersTable".',
position: '2218',
file: 'parse_relation.c',
line: '3597',
routine: 'errorMissingRTE'
}
PostgresError: invalid reference to FROM-clause entry for table "orders"
at ErrorResponse
at handle
at Socket.data
at Socket.emit (node:events:514:28)
at addChunk (node:internal/streams/readable:376:12)
at readableAddChunk (node:internal/streams/readable:349:9)
at Readable.push (node:internal/streams/readable:286:10)
at TCP.onStreamRead (node:internal/stream_base_commons:190:23)
at TCP.callbackTrampoline (node:internal/async_hooks:130:17) {
severity_local: 'ERROR',
severity: 'ERROR',
code: '42P01',
hint: 'Perhaps you meant to reference the table alias "ordersTable".',
position: '2218',
file: 'parse_relation.c',
line: '3597',
routine: 'errorMissingRTE'
}
It was working before I changed the const name of my table from const orders = pgTable("orders"...) to const ordersTable = pgTable("orders"...)

Did you find this page helpful?