Skip to content

UpsertGraph deletes rows for relation which is not mentioned in graph #1455

@lynxtaa

Description

@lynxtaa

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?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions