Using the WITH clause with inserts

Hi. I'm trying to use the WITH clause with inserts.
const createdAddress = this.drizzleService.db
.$with('created_address')
.as(
this.drizzleService.db
.insert(databaseSchema.addresses)
.values(user.address)
.returning(),
);
const createdAddress = this.drizzleService.db
.$with('created_address')
.as(
this.drizzleService.db
.insert(databaseSchema.addresses)
.values(user.address)
.returning(),
);
Unfortunately, I'm seeing the following error. Looks like $with() is not ready to use with inserts. Or maybe I'm missing something?
2 Replies
Marcin Wanago
Marcin WanagoOP6mo ago
error TS2345: Argument of type 'Omit<PgInsertBase<PgTableWithColumns<{ name: "addresses"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "addresses"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; street: PgCo...' is not assignable to parameter of type 'TypedQueryBuilder<ColumnsSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection, unknown>)'.
Type 'Omit<PgInsertBase<PgTableWithColumns<{ name: "addresses"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "addresses"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; street: PgCo...' is not assignable to type 'TypedQueryBuilder<ColumnsSelection, unknown>'.
Types of property '_' are incompatible.
Property 'selectedFields' is missing in type '{ readonly dialect: "pg"; readonly table: PgTableWithColumns<{ name: "addresses"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "addresses"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; ...' but required in type '{ selectedFields: ColumnsSelection; result: unknown; }'.

67 this.drizzleService.db
~~~~~~~~~~~~~~~~~~~~~~
68 .insert(databaseSchema.addresses)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 .values(user.address)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 .returning(),
~~~~~~~~~~~~~~~~~~~~~~

node_modules/drizzle-orm/query-builders/query-builder.d.ts:6:9
6 selectedFields: TSelection;
~~~~~~~~~~~~~~
'selectedFields' is declared here.
error TS2345: Argument of type 'Omit<PgInsertBase<PgTableWithColumns<{ name: "addresses"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "addresses"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; street: PgCo...' is not assignable to parameter of type 'TypedQueryBuilder<ColumnsSelection, unknown> | ((qb: QueryBuilder) => TypedQueryBuilder<ColumnsSelection, unknown>)'.
Type 'Omit<PgInsertBase<PgTableWithColumns<{ name: "addresses"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "addresses"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; }, {}, {}>; street: PgCo...' is not assignable to type 'TypedQueryBuilder<ColumnsSelection, unknown>'.
Types of property '_' are incompatible.
Property 'selectedFields' is missing in type '{ readonly dialect: "pg"; readonly table: PgTableWithColumns<{ name: "addresses"; schema: undefined; columns: { id: PgColumn<{ name: "id"; tableName: "addresses"; dataType: "number"; columnType: "PgSerial"; data: number; driverParam: number; notNull: true; hasDefault: true; enumValues: undefined; baseColumn: never; ...' but required in type '{ selectedFields: ColumnsSelection; result: unknown; }'.

67 this.drizzleService.db
~~~~~~~~~~~~~~~~~~~~~~
68 .insert(databaseSchema.addresses)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
69 .values(user.address)
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
70 .returning(),
~~~~~~~~~~~~~~~~~~~~~~

node_modules/drizzle-orm/query-builders/query-builder.d.ts:6:9
6 selectedFields: TSelection;
~~~~~~~~~~~~~~
'selectedFields' is declared here.
Artamiel
Artamiel6mo ago
Hi, I checked the declaration files and it seems it only support select query as the subquery as of now
No description
Want results from more Discord servers?
Add your server