Lostra
Lostra
DTDrizzle Team
Created by Lostra on 3/13/2024 in #help
"onConflictDoUpdate" fails with error in Array.flatMap
I am trying to run an upsert function, and update the values if it already exists. Now I might be completely in the wrong with the code. I have made a base "repository" class that has the upsert function simplified as follows:
protected async upsert(
data: typeof this.schema.$inferInsert | typeof this.schema.$inferInsert[]
){
return await this.repository.insert(this.schema).values(data).onConflictDoUpdate({
target: this.schema.id,
set: data
});
}
protected async upsert(
data: typeof this.schema.$inferInsert | typeof this.schema.$inferInsert[]
){
return await this.repository.insert(this.schema).values(data).onConflictDoUpdate({
target: this.schema.id,
set: data
});
}
now I have tried the upsert in a loop for the array is the data happens to be an array, did not do anything, getting the same error.
Cannot read properties of undefined (reading 'name')
at file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:921
at Array.flatMap (<anonymous>)
at UDe.buildUpdateSet (file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:848)
at QueryPromise.onConflictDoUpdate (file:///var/task/shared/modules/syncer/handlers/sync.mjs:723:85175)
at $De.upsert (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:3003)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async $De.insertScan (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:6785)
Cannot read properties of undefined (reading 'name')
at file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:921
at Array.flatMap (<anonymous>)
at UDe.buildUpdateSet (file:///var/task/shared/modules/syncer/handlers/sync.mjs:729:848)
at QueryPromise.onConflictDoUpdate (file:///var/task/shared/modules/syncer/handlers/sync.mjs:723:85175)
at $De.upsert (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:3003)
at process.processTicksAndRejections (node:internal/process/task_queues:95:5)
at async $De.insertScan (file:///var/task/shared/modules/syncer/handlers/sync.mjs:730:6785)
4 replies
DTDrizzle Team
Created by Lostra on 2/27/2024 in #help
Insert silently fails with missing table name?
Hi everyone! I've been using Drizzle the past few days and so far it's been a breeze. However, I've ran into an issue today that I cannot seem to figure out. I have a schema file that contains table definitions, like:
export const clients = pgTable("clients", {
id: text("id").primaryKey().notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
company: text("company").notNull().unique(),
// The rest of the columns
});

export type Client = typeof clients.$inferSelect;
export type NewClient = typeof clients.$inferInsert;

export const accounts = pgTable("accounts", {
id: text("id").primaryKey().notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
email: text("email").unique().notNull(),
// The rest of the columns
});
export const clients = pgTable("clients", {
id: text("id").primaryKey().notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
company: text("company").notNull().unique(),
// The rest of the columns
});

export type Client = typeof clients.$inferSelect;
export type NewClient = typeof clients.$inferInsert;

export const accounts = pgTable("accounts", {
id: text("id").primaryKey().notNull(),
createdAt: timestamp("created_at").defaultNow().notNull(),
email: text("email").unique().notNull(),
// The rest of the columns
});
I am using RDS Data API with Drizzle. Created a repository service class for Clients and Accounts as well. I have the necessary functions such as getters, inserts, etc. For example, this is how a new client is inserted into the database:
public async insertClient(client: NewClient) {
if (!this.repository) {
await this.createRepositoryAccess(); // Ensure that drizzle() is called already
}

return this.repository
.insert(clients)
.values(client)
.returning({
id: clients.id,
createdAt: clients.createdAt,
});
}
public async insertClient(client: NewClient) {
if (!this.repository) {
await this.createRepositoryAccess(); // Ensure that drizzle() is called already
}

return this.repository
.insert(clients)
.values(client)
.returning({
id: clients.id,
createdAt: clients.createdAt,
});
}
This works perfectly, the client is inserted, all good. When I try to do the exact same thing with the "accounts" table however, the insert fails silently with 0 logs, 0 errors (callers are wrapped with try/catch), and as return value, I just get an empty object. The "accounts" insert method:
public async createNewAccount(accountData: NewAccount) {
if (!this.repository) {
await this.createRepositoryAccess();
}

return this.repository
.insert(accounts)
.values(accountData)
.returning({
id: accounts.id,
createdAt: accounts.createdAt,
});
}
public async createNewAccount(accountData: NewAccount) {
if (!this.repository) {
await this.createRepositoryAccess();
}

return this.repository
.insert(accounts)
.values(accountData)
.returning({
id: accounts.id,
createdAt: accounts.createdAt,
});
}
Anyone has ever seen something like this?
2 replies