Objection.js: $afterUpdate does not contain enough data when using $relatedQuery

Created on 12 Jun 2018  路  6Comments  路  Source: Vincit/objection.js

Hi,

I'm using objectionsjs for about a month (so I may be missing some tricks).
But when I use $relatedQuery to link a model to another, $afterUpdate is triggered correctly but does not contain all the information, even if I'm pretty sure objection has it somewhere.

Example

const idToLink = 1234;
const one = await One.query().where({ id: 1 }).first();
one.$relatedQuery('two').relate(idToLink);

## trigger => one.$afterUpdate(queryContext);
queryContext = { patch: true };
this = { two_id: 1234 }; 

I don't know it's clear, but I fetched model one and link a belongTo relation two
when the one.$afterUpdate is triggered, the queryContext contain nothing usefull and this contain only what changed. The model should be a copy of the one queried I suppose.
In my case, I'm executing sensitive task in afterUpdate, if this afterUpdate does not contain the ID, I can not keep my task up to date :/

Maybe I'm also doing something wrong, but I'm following the doc.

Thanks :)

Most helpful comment

Getting an id in the beforeUpdate and afterUpdate methods is important for me.

All 6 comments

$afterUpdate(opt, queryContext) {
  console.log(opt.old.id)
}

Thanks for the quick answer, but this is not true :/
Here is the full repro (with a PG, you can change the conf) maybe you will understand it better :)

# repro.js
const knex = require('knex');
var { Model } = require('objection');


class Test1 extends Model {
  static tableName() {
    return 'Test1';
  }
}
class Test2 extends Model {
  static tableName() {
    return 'Test2';
  }
  static get relationMappings() {
    return {
      test: {
        relation: Model.BelongsToOneRelation,
        modelClass: Test1,
        join: {
          from: 'Test1.id',
          to: 'Test2.test1Id',
        },
      },
    };
  }

  $afterUpdate(opt) {
    console.log('------- this will print undefined =>', opt.old);
  }
}

const connection = knex({
  client: 'postgresql',
  debug: true,
  connection: {
    host: 'localhost',
    user: 'api',
    password: 'api1234',
    database: 'api',
    port: 5432,
  },
  // connection: 'postgresql://api:blabla@localhost:5432/api',
  migrations: {
    tableName: 'knex_migrations',
    directory: './migrations',
  },
  seeds: {
    tableName: 'knex_seeds',
    directory: './seeds',
  },
});
Model.knex(connection);

const run = async function() {
  const hasTable1 = await connection.schema.hasTable('Test1');
  if (!hasTable1) {
    await connection.schema.createTable('Test1', (table) => {
      table
        .increments('id')
        .unsigned()
        .primary();
    });
  }
  const hasTable2 = await connection.schema.hasTable('Test2');
  if (!hasTable2) {
    await connection.schema.createTable('Test2', (table) => {
      table
        .increments('id')
        .unsigned()
        .primary();
      table
        .integer('test1Id')
        .unsigned()
        .references('id')
        .inTable('Test1')
        .nullable();
    });
  }

  const m1 = await Test1.query().insert({}).returning('*');
  const m2 = await Test2.query().insert({}).returning('*');

  const m3 = await m2.$relatedQuery('test').relate(m1.id);
};

run();
node repro.js

Damn, you are right. Unfortunately there is no way to do that currently then. I'm designing new hooks for 2.0 release that would make this possible among other things, but 2.0 is still far away.

Hello!
There was this commit https://github.com/Vincit/objection.js/commit/5d841fead5d36ce508a38a4eecf231a537126f6f.
He kind of solved the problem.

Maybe it's worth reopening ? :)

Getting an id in the beforeUpdate and afterUpdate methods is important for me.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

Gustav0ar picture Gustav0ar  路  4Comments

officer-rosmarino picture officer-rosmarino  路  4Comments

chen7david picture chen7david  路  3Comments

rickmed picture rickmed  路  4Comments

mycahjay-nms picture mycahjay-nms  路  4Comments