how to set cascade `SET NULL` inside the schema.pr...
# orm-help
d
how to set cascade
SET NULL
inside the schema.prisma? Is the only way of doing this an optional relation?
r
@Dev__ 👋 Currently we are working on allowing to set cascade options, so for now you could use something like this.
d
thanks
so I tried it but it doesnt really work, maybe I am doing smething wrong
Copy code
// schema
model Buyer {
  ...
  
  /// @onDelete(SET_NULL)
  quotations Quotation[]
}

model Quotation {
  ...

   buyer Buyer @relation(fields: [buyerId], references: [id])
}

// code
const prismaDelete = new PrismaDelete(this.prisma);

await prismaDelete.onDelete({
	model: 'Buyer',
	where: {
		buyerId: {
			in: ids
		}
	}
});
I am still getting an error
r
@Ahmed can help you here 🙂
👍 1
a
@Dev__ where argument must be accepted by buyer So I think it will be
Copy code
Where:{id:{in:ids}}
d
@Ahmed yes youre right, that was my mistake, but I still get the same error
Copy code
await prismaDelete.onDelete({
	model: 'Buyer',
	where: {
		id: {
			in: ids
		}
	}
});
a
What I see in your model that buyer is required. I think the issue we can not make it null because it's required Try to make it optional or make cascade delete not set null
d
hmm, so I set it to optional relation but now it is deleting the relation row instead of setting FK to
null
🤔
a
Copy code
// schema
model Buyer {
  ...
  Delete all relation when delete 
  /// @onDelete(CASCADE) 
  quotations Quotation[]
}
Copy code
model Quotation {
  ...
Copy code
//or make this relation optional 
buyer Buyer? @relation(fields: [buyerId], references: [id])
}
d
i dont want to delete it, i thought the SET_NULL would set the FK to null, not delete it
a
Ok so you need to make the relationship optional
d
yes I did, but using
SET_NULL
will still delete the relation records. So i dont need to use it then
a
No SET-NULL does not delete. Just disconnect the relationship
d
in my case it does delete them
a
You can see my plugin source https://github.com/paljs/prisma-tools/blob/main/packages/plugins/src/delete.ts line 104. You will understand what I mean
d
hmm strange, for some reason it get deleted...
a
Please be sure you run prisma generate after every edit in your schema.prisma file
And also, restart your server
d
yes, I am doing it, but same behaviour...
I am not running other queries that is deleting the relation, relation is also optional now
Copy code
model Quotation {
    id                String   @id @db.VarChar(255)
    buyerId           String   @db.VarChar(255)

    buyer Buyer? @relation(fields: [buyerId], references: [id])
}

/// @onDelete(SET_NULL)
quotations           Quotation[]
a
Wait, if the relationship optional, the buyerId field must be optional also And don't forget to push this to your DB
d
yes, I am doing it all, push, generate, restart server. weird...
I made the field optional as wel but nothing 😞
d
Hey @Dev__, I didn’t read the whole thread here, but wanted to share that in addition to the great Ahmed is doing, there’s also a proposal for defining the onDelete and onUpdate behaviour which should be available in the near future: https://github.com/prisma/prisma/issues/6996
d
Thanks for the info. My last guess would be that the changes are not properly pushed when using
push
. Will try again with deleting the database and running a fresh
push