Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

upsertMany to table with a composite key #4923

Closed
bobgubko opened this issue Nov 14, 2023 · 0 comments
Closed

upsertMany to table with a composite key #4923

bobgubko opened this issue Nov 14, 2023 · 0 comments

Comments

@bobgubko
Copy link

Describe the bug

ORM executes invalid SELECT query after upsertMany to table with a composite primary key. It works fine with upsert.

It seems problem is in additional non-primary column in table. It works if I remove createdAt column.

Stack trace

 DriverException: select `u0`.`user_id`, `u0`.`action`, `u0`.`created_at` from `user_actions` as `u0` where (`u0`.`user_id`, `u0`.`action`) is null - Operand should contain 1 column(s)

at MySqlExceptionConverter.convertException (node_modules/@mikro-orm/core/platforms/ExceptionConverter.js:8:16)
      at MySqlExceptionConverter.convertException (node_modules/@mikro-orm/mysql/MySqlExceptionConverter.js:80:22)
      at MySqlDriver.convertException (node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:201:54)
      at node_modules/@mikro-orm/core/drivers/DatabaseDriver.js:205:24
      at SqlEntityManager.upsertMany (node_modules/@mikro-orm/core/EntityManager.js:690:27)
      at Object.<anonymous> (json.test.ts:56:18)
      previous Error: select `u0`.`user_id`, `u0`.`action`, `u0`.`created_at` from `user_actions` as `u0` where (`u0`.`user_id`, `u0`.`action`) is null - Operand should contain 1 column(s)
      at Packet.asError (node_modules/mysql2/lib/packets/packet.js:728:17)
      at Query.execute (node_modules/mysql2/lib/commands/command.js:29:26)
      at Connection.handlePacket (node_modules/mysql2/lib/connection.js:478:34)
      at PacketParser.onPacket (node_modules/mysql2/lib/connection.js:97:12)
      at PacketParser.executeStart (node_modules/mysql2/lib/packet_parser.js:75:16)
      at Socket.<anonymous> (node_modules/mysql2/lib/connection.js:104:25)

To Reproduce

@Entity()
class User {
  @PrimaryKey()
  id!: number;

  @Property()
  name!: string;
}

export type Actions = 'add' | 'delete';

@Entity() 
export class UserActions {
  @ManyToOne(() => User, {
    primary: true,
    onDelete: 'cascade',
  })
  user!: User

  @Enum({
    items: () => ['add', 'delete'],
    primary: true,
  })
  action!: Actions

  [PrimaryKeyType]?: [number, Actions] // this is needed for proper type checks in `FilterQuery`

  @Property({ columnType: 'timestamp', defaultRaw: `CURRENT_TIMESTAMP` })
  createdAt?: Date;
}

test('It should upsert many', async() => {
  const orm = await MikroORM.init({
    type: 'mysql',
    dbName: 'mo-test',
    host: '127.0.0.1',
    user: 'root',
    password: '123',
    port: 3306,
    allowGlobalContext: true,
    entities: [User, UserActions],
    metadataProvider: TsMorphMetadataProvider,
    debug: true,
  });

  await orm.schema.refreshDatabase();

  orm.em.create(User, { id: 1, name: 'test' });

  await orm.em.flush();

  const result = await orm.em.upsertMany(UserActions, [
    {
      user: 1,
      action: 'add',
    },
  ])

  expect(result).toHaveLength(1);
});

Versions

Dependency Version
node 18.12.1
typescript 5.2.2
mikro-orm 5.9.3
your-driver mysql
@B4nan B4nan closed this as completed in 0ba5b6e Nov 15, 2023
B4nan added a commit that referenced this issue Nov 17, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant