Hi,
is there a way to eager delete rows with objection ?
Yes, by using ON DELETE CASCADE in migrations for your foreign keys. Doing cascade deletes outside the database is super error-prone.
ON DELETE CASCADE does not always do the job, though, if the child rows are still related to some "uncle" rows.
WITH RECURSIVE is the right solution to in that case.
That is kind of interesting idea, even that it cannot replace db level cascade. Would implementing this require to modify algorithm that figures out eagering graph and then just launch deletes instead of select?
I'm writing a new hook API for objection 2.0. With the new static beforeDelete hook, you could do this:
class Person extends Model {
static async beforeDelete({ findQuery, transaction }) {
// `findQuery` is a "read-only" version of the delete query about to be executed.
// You can use it for example as a subquery like this:
await Pet.query(transaction).delete().whereIn('ownerId', findQuery.select('id'))
}
}
Most helpful comment
I'm writing a new hook API for objection 2.0. With the new static
beforeDeletehook, you could do this: