drizzle-orm
drizzle-orm copied to clipboard
[BUG]: can't insert to relative table in transaction
What version of drizzle-orm are you using?
0.29.3
What version of drizzle-kit are you using?
0.20.14
Describe the Bug
I have 2 Postgres tables connected by foreign key and I want to insert data in both of them inside drizzle transaction. Insert values in second table depend on generated id from the first.
And I get foreign key error - "PostgresError: insert or update on table "dbtMergeConflictFiles" violates foreign key constraint "dbtMergeConflictFiles_dbtMergeConflictId_dbtMergeConflicts_id_f"". Here is my schema and transaction code:
export const dbtMergeConflicts = pgTable('dbtMergeConflicts', {
id: uuid('id').primaryKey().defaultRandom(),
dbtProjectId: uuid('dbtProjectId').references(() => dbtProjects.id).notNull(),
})
export const dbtMergeConflictFiles = pgTable('dbtMergeConflictFiles', {
id: uuid('id').primaryKey().defaultRandom(),
dbtMergeConflictId: uuid('dbtMergeConflictId').references(() => dbtMergeConflicts.id).notNull(),
filePath: text('filePath').notNull()
})
await db.transaction(async tx => {
const res = await tx.insert(dbtMergeConflicts).values({
dbtProjectId: projectId
}).returning({id: dbtMergeConflicts.id})
console.log(res) // [ { id: '7400c246-d82b-45ef-8796-32ddedbdb51e' } ]
await db.insert(dbtMergeConflictFiles).values(
{
dbtMergeConflictId: res[0]!.id,
filePath: 'some_path'
}
)
})
Expected behavior
No response
Environment & setup
No response