Objection.js: eager delete ?

Created on 20 Apr 2019  路  4Comments  路  Source: Vincit/objection.js

Hi,

is there a way to eager delete rows with objection ?

Most helpful comment

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'))
  }
}

All 4 comments

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'))
  }
}
Was this page helpful?
0 / 5 - 0 ratings

Related issues

AhmadRaza786 picture AhmadRaza786  路  3Comments

nazar picture nazar  路  3Comments

bsdo64 picture bsdo64  路  3Comments

sgangwisch picture sgangwisch  路  4Comments

rickmed picture rickmed  路  4Comments