Skip to content

Unnecessary id update query in "upsertGraph" #480

@hirikarate

Description

@hirikarate

ObjectionJS: 0.8.6
Knex: 0.13.0
NodeJS: 7.10.1

I have two tables with many to many relationship:

programs(id, name)
groups(id, name)
program_group(program_id, group_id)

These are mapped models:

class Group {
   ....
}

class Program {
  static relationMappings = {
    groups: {
      relation: Model.ManyToManyRelation,
      modelClass: Group,
      join: {
        from: 'programs.id',
        through: {
          from: program_group.program_id',
          to: ''program_group.group_id' 
        },
        to: 'groups.id'
      }
    }
  }
  ....
}

When I run this code

Program.query().upsertGraph({
    id: programId,
    groups: {
      id: groupId
    }
  },
  {
    relate: true,
    unrelate: true
  })

The executed queries are:
select "programs"."id" from "programs" where "programs"."id" in ($1)

select "program_group"."program_id" as "objectiontmpjoin0", "groups"."id" from "groups" inner join "program_group" on "program_group"."group_id" = "groups"."id" where "program_group"."program_id" in ($1)

insert into "program_group" ("group_id", "program_id") values ($1, $2) returning "program_id"

update "programs" set "id" = $1 where "programs"."id" = $2


My questions:

  1. The last update seems to be unnecessary, does it?
  2. I just want to execute the insert into "program_group" (kinda blindly insert) without the other above queries. How do I achieve that?

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions