nested select type issue

hello there... this code returns desired restuls but i have type issues
await db
.select({
...getTableColumns(productTable),
variant: {
...getTableColumns(variantTable),
size: getTableColumns(sizeTable),
color: getTableColumns(colorTable),
},
brand: getTableColumns(brandTable),
category: getTableColumns(categoryTable),
})
.from(productTable)
.where(eq(productTable.id, +id))
.leftJoin(
variantTable,
eq(variantTable.product_id, productTable.id),
)
.leftJoin(brandTable, eq(brandTable.id, productTable.brand_id))
.leftJoin(
categoryTable,
eq(categoryTable.id, productTable.category_id),
)
.leftJoin(colorTable, eq(colorTable.id, variantTable.color_id))
.leftJoin(sizeTable, eq(sizeTable.id, variantTable.size_id));
await db
.select({
...getTableColumns(productTable),
variant: {
...getTableColumns(variantTable),
size: getTableColumns(sizeTable),
color: getTableColumns(colorTable),
},
brand: getTableColumns(brandTable),
category: getTableColumns(categoryTable),
})
.from(productTable)
.where(eq(productTable.id, +id))
.leftJoin(
variantTable,
eq(variantTable.product_id, productTable.id),
)
.leftJoin(brandTable, eq(brandTable.id, productTable.brand_id))
.leftJoin(
categoryTable,
eq(categoryTable.id, productTable.category_id),
)
.leftJoin(colorTable, eq(colorTable.id, variantTable.color_id))
.leftJoin(sizeTable, eq(sizeTable.id, variantTable.size_id));
error
Type '{ id: PgColumn<{ name: "id"; tableName: "sizes"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; store_id: PgColumn<...>; }' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>'.
Type '{ id: PgColumn<{ name: "id"; tableName: "sizes"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; store_id: PgColumn<...>; }' is missing the following properties from type 'PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>': table, _, keyAsName, primary, and 18 more.ts(2322)
Type '{ id: PgColumn<{ name: "id"; tableName: "sizes"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; store_id: PgColumn<...>; }' is not assignable to type 'SQL<unknown> | Aliased<unknown> | PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>'.
Type '{ id: PgColumn<{ name: "id"; tableName: "sizes"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; isPrimaryKey: true; isAutoincrement: false; ... 4 more ...; generated: undefined; }, {}, {}>; name: PgColumn<...>; store_id: PgColumn<...>; }' is missing the following properties from type 'PgColumn<ColumnBaseConfig<ColumnDataType, string>, {}, {}>': table, _, keyAsName, primary, and 18 more.ts(2322)
2 Replies
Mario564
Mario5642mo ago
The query builder currently has the limitation where the result can only have two levels of nesting, so you'll have to rewrite the variant object make it into 3 objects: variant, variantBrand, variantColor.
M Zeeshan
M ZeeshanOP2mo ago
Thx ❤️

Did you find this page helpful?