Bryan3
Bryan3
Explore posts from servers
DTDrizzle Team
Created by Bryan3 on 9/29/2023 in #help
Delete operation in transaction sometimes not deleting row in database
Hi, I'm calling a delete operation in a transaction that deletes an organization row in a Supabase Postgres database:
let data: unknown[] = [];
await db.transaction(async (tx) => {
// Update the organizationId of all related contacts to new id
await tx
.update(contact)
.set({ organizationId: input.newId })
.where(eq(contact.organizationId, input.id));
// Delete the organization with old id
data = await tx
.delete(organization)
.where(eq(organization.id, input.id))
.returning();
});
console.log("deleting organiation with id: ", input.id);
console.log("organization deleted: ", data);
let data: unknown[] = [];
await db.transaction(async (tx) => {
// Update the organizationId of all related contacts to new id
await tx
.update(contact)
.set({ organizationId: input.newId })
.where(eq(contact.organizationId, input.id));
// Delete the organization with old id
data = await tx
.delete(organization)
.where(eq(organization.id, input.id))
.returning();
});
console.log("deleting organiation with id: ", input.id);
console.log("organization deleted: ", data);
But sometimes if I delete the row immediately after creation, the console log says the row is deleted but it remains in the database. I need to call delete again for the row to be deleted. (Two deletions console logged below where only after the second delete log row is deleted from the database).
organization created: [
{
id: '9e0d585b-8284-4181-b7bf-6ec879700c6e',
name: '121',
createdAt: 2023-09-30T00:19:26.979Z,
updatedAt: 2023-09-30T00:19:26.979Z
}
]
organization deleted: [
{
id: '9e0d585b-8284-4181-b7bf-6ec879700c6e',
name: '121',
createdAt: 2023-09-30T00:19:26.979Z,
updatedAt: 2023-09-30T00:19:26.979Z
}
]
organization deleted: [
{
id: '9e0d585b-8284-4181-b7bf-6ec879700c6e',
name: '121',
createdAt: 2023-09-30T00:19:26.979Z,
updatedAt: 2023-09-30T00:19:26.979Z
}
]
organization created: [
{
id: '9e0d585b-8284-4181-b7bf-6ec879700c6e',
name: '121',
createdAt: 2023-09-30T00:19:26.979Z,
updatedAt: 2023-09-30T00:19:26.979Z
}
]
organization deleted: [
{
id: '9e0d585b-8284-4181-b7bf-6ec879700c6e',
name: '121',
createdAt: 2023-09-30T00:19:26.979Z,
updatedAt: 2023-09-30T00:19:26.979Z
}
]
organization deleted: [
{
id: '9e0d585b-8284-4181-b7bf-6ec879700c6e',
name: '121',
createdAt: 2023-09-30T00:19:26.979Z,
updatedAt: 2023-09-30T00:19:26.979Z
}
]
Is there an issue with how the transaction is written that is causing the problem? It seems that if I'm getting a console log back drizzle is telling me the object should be deleted. Thank you very much.
6 replies
DTDrizzle Team
Created by Bryan3 on 9/28/2023 in #help
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.
10 replies