Objection.js: UpsertGraph deletes rows for relation which is not mentioned in graph

Created on 30 Aug 2019  路  8Comments  路  Source: Vincit/objection.js

I'm using [email protected] and have these models:

class Role extends Model {
  static get tableName() {
    return 'roles'
  }

  static get relationMappings() {
    return {
      sets: {
        relation: Model.HasManyRelation,
        modelClass: 'Set',
        join: { from: 'roles.id', to: 'sets.roleId' },
      },
    }
  }
}

class Set extends Model {
  static get tableName() {
    return 'sets'
  }

  static get relationMappings() {
    return {
      setAttributes: {
        relation: Model.HasManyRelation,
        modelClass: 'SetAttribute',
        join: { from: 'sets.id', to: 'sets_attributes.setId' },
      },
    }
  }
}

class SetAttribute extends Model {
  static get tableName() {
    return 'sets_attributes'
  }
}

Suppose role with ID 1 already has set with ID 1.

If I'm using upsertGraph like this, It works correctly -- new row is inserted in "sets" table:

transaction(Role.knex(), trx =>
  Role.query(trx).upsertGraph({
    id: 1,
    sets: [{ id: 1 }, { name: 'New Set' }],
  }),
)

But if I pass "setAttributes" for newly created set, Objection deletes all setAttributes for the first set (with id=1):

transaction(Role.knex(), trx =>
  Role.query(trx).upsertGraph({
    id: 1,
    sets: [
      { id: 1 },
      { name: 'New Set', setAttributes: [{ name: 'New Set Attribute' }] },
    ],
  }),
)

Is this behaviour expected?

bug

Most helpful comment

Oh man, this was a pretty nasty bug! I can't believe why nobody reported this earlier. objection 1.6.10 will be released in couple of minutes.

All 8 comments

Could you provide a reproduction for this? I don't believe this is really happening. There are tests for this case. I'll fix it immediately if you can provide a reproduction.

Thanks for a quick response! I've created reproducible example: https://github.com/lynxtaa/objection-issue

Thanks for the repro! I'll see what's going on asap.

@koskimas any updates on this?

I'll post here once I find time to work on this.

Fix coming up soon. Sorry it took so long.

Oh man, this was a pretty nasty bug! I can't believe why nobody reported this earlier. objection 1.6.10 will be released in couple of minutes.

@koskimas Thank you! We are using Objection.js in production and very happy with it

Was this page helpful?
0 / 5 - 0 ratings