Where does the onDelete on relation is applying ?

For instance :
const product = pgTable('product', {
logistics_id: serial('logistics_id')
.references(() => logistics.id, { onDelete: 'cascade' })
.notNull(),
})
const product = pgTable('product', {
logistics_id: serial('logistics_id')
.references(() => logistics.id, { onDelete: 'cascade' })
.notNull(),
})
Here the cascade delete will occur when I delete my product entity (and so delete the logistic linked to it) Or when I delete the logistic (will delete the product linked to it)
10 Replies
Angelelz
Angelelz15mo ago
In a one-to-many relation, if you use on delete cascade, only by deleting the one, will delete the many. Not the other way In your case, if you delete a logistics, its corresponding product will be deleted
Louistiti
LouistitiOP15mo ago
Thanks for your answer ! So I have to delete manually logistics on product delete ?
Angelelz
Angelelz15mo ago
I can't answer that question, it's kind of a business logic question. I don't know what is the architecture of your app.
Louistiti
LouistitiOP15mo ago
Alright, thanks anyway !
Angelelz
Angelelz15mo ago
Is this a one to one or a one to many relation?
Louistiti
LouistitiOP15mo ago
one to one but my logistics table is used with other tables as well but as using .references() makes the relation mandatory (cannot be null) I cannot put an onDelete on my product table's side
Angelelz
Angelelz15mo ago
Adding notNull() makes the relation mandatory. The references shouldn't make it mandatory
Louistiti
LouistitiOP15mo ago
it shouldn't but it does unfortunately
Louistiti
LouistitiOP15mo ago
GitHub
[BUG]: A foreign key can not be null · Issue #1264 · drizzle-team/...
What version of drizzle-orm are you using? 0.28.6 What version of drizzle-kit are you using? 0.19.13 Describe the Bug I have schema users table like this export const users = pgTable('users'...
Louistiti
LouistitiOP15mo ago
When I try to create an entity without specifying a relation, I get a foreign key constraint error which is fixed when I remove the .references()
Want results from more Discord servers?
Add your server