sequelize-typescript
sequelize-typescript copied to clipboard
Upsert don't return the good Id when already exist
Issue
(Idk if it's a sequelize-typescript or a sequelize issue)
Versions
- sequelize: 6.19.0
- sequelize-typescript: 2.1.3
- typescript: 4.6.3
Issue type
- [x] bug report
- [ ] feature request
Actual behavior
Using <Model>.upsert({unique}), when unique is a Unique field and already exist in table, return a new generated Id.
Expected behavior
Returning the already existing row.
Steps to reproduce
- Create a model with a unique field
- Using
<Model>.upsert({unique})whenuniqueis already present in table (upsert also correctly return that he found the row and uptaded it)
Related code
Statement.ts
import { Column, CreatedAt, DataType, Default, HasMany, Model, PrimaryKey, Table, Unique, UpdatedAt } from "sequelize-typescript";
import { SensorMesurement } from "./models.js"
@Table({ tableName: "statement" })
export class Statement extends Model {
@PrimaryKey
@Default(DataType.UUIDV4)
@Column(DataType.UUID)
declare id: string;
@CreatedAt
@Column
declare created_at: Date;
@UpdatedAt
@Column
declare updated_at: Date;
@Column
declare date: Date;
@Unique
@Column
declare test: string;
@HasMany(() => SensorMesurement, "statement_id")
declare sensorMesurements: SensorMesurement[];
}
index.ts
import { initBDD } from "../database/database.js";
import { Statement } from "../database/models/models.js";
await initBDD();
(async () => {
const [statement, isNew] = await Statement.upsert({date: Date.now(), test: "Oui"});
console.log(statement, isNew);
/*
First time it will return {id: 'fa6bcac5-fbdd-433a-b5e8-7d8fb1afb151', date: 2022-04-22T13:38:49.173Z, test: 'Oui'} with isNew = true and correctly creating the row (cf. pic1)
Second time it will return {id: 'd1cfb3f4-47ca-4941-8d20-fc30fd5ccc39', date: 2022-04-22T13:41:11.436Z, test: 'Oui'} with isNew = false
and correctly updating the row (cf pic2) but that's not the good Id that was returned
My current only fix is to use findOne then create or update manually
*/
})();
Pic1

Pic2
