How to include all fields in partial select syntax

Hi, I'm trying to append the organization field using a leftjoin to the existing fields in contact like this:
const response = (await db
.select({
...contact,
organization: {
name: organization.name,
},
})
.from(contact)
.leftJoin(
organization,
eq(contact.organizationId, organization.id),
))
const response = (await db
.select({
...contact,
organization: {
name: organization.name,
},
})
.from(contact)
.leftJoin(
organization,
eq(contact.organizationId, organization.id),
))
The query works but the IDE is giving me the error:
Argument of type '{ organization: { name: PgColumn<{ name: "name"; tableName: "Organization"; dataType: "string"; columnType: "PgText"; data: string; driverParam: string; notNull: true; hasDefault: false; enumValues: [string, ...string[]]; baseColumn: never; }, {}, {}>; }; ... 11 more ...; updatedAt: PgColumn<...>; }' is not assignable to parameter of type 'SelectedFields'.
Property '_' is incompatible with index signature.
Argument of type '{ organization: { name: PgColumn<{ name: "name"; tableName: "Organization"; dataType: "string"; columnType: "PgText"; data: string; driverParam: string; notNull: true; hasDefault: false; enumValues: [string, ...string[]]; baseColumn: never; }, {}, {}>; }; ... 11 more ...; updatedAt: PgColumn<...>; }' is not assignable to parameter of type 'SelectedFields'.
Property '_' is incompatible with index signature.
for the ...contact part. What would be the correct syntax to do this? Thanks.
6 Replies
Bryan3
Bryan3OP14mo ago
I used getTableColumns() to solve the issue
const { ...rest } = getTableColumns(contact);

const response = (await db
.select({
...rest,
organization: {
name: organization.name,
},
})
.from(contact)
.leftJoin(
organization,
eq(contact.organizationId, organization.id),
))
const { ...rest } = getTableColumns(contact);

const response = (await db
.select({
...rest,
organization: {
name: organization.name,
},
})
.from(contact)
.leftJoin(
organization,
eq(contact.organizationId, organization.id),
))
Angelelz
Angelelz14mo ago
import { getTableColumns } from "drizzle-orm";
import { getTableColumns } from "drizzle-orm";
and then you do:
const { ...rest } = getTableColumns(contact);

const response = (await db
.select({
...getTableColumns(contact),
organization: {
name: organization.name,
},
})
.from(contact)
.leftJoin(
organization,
eq(contact.organizationId, organization.id),
))
const { ...rest } = getTableColumns(contact);

const response = (await db
.select({
...getTableColumns(contact),
organization: {
name: organization.name,
},
})
.from(contact)
.leftJoin(
organization,
eq(contact.organizationId, organization.id),
))
Oh, you got it
Bryan3
Bryan3OP14mo ago
Thanks, found it searching the earlier questions on the server
G$Row
G$Row10mo ago
Is there a way to do this with the columns of a subquery?
Angelelz
Angelelz10mo ago
Not yet, there an open pr
Want results from more Discord servers?
Add your server