Version sequelize 6.37.3
I try to upsert() myModel
but the mehtod returns:
TypeError: model.upsert is not a function
my_model.controller.ts
...
class MyModelController {
private model: any;
...
async put(req: Request, res: Response) {
// get stored model
const model = await this.model.findByPk(currentId, {
attributes: ['id'],
include: {
model: MyModelStatus,
attributes: ['name']
}
});
// set model attributes
...
// validate model
...
// execute query
const [instance, created] = await model.upsert({}); // ERROR ===> model.upsert is not a function
console.log(instance, created)
}
myModel holds base data and objects like status, address etc.
Below shown the model myModel
instance:
MyModel {
dataValues: {
id: '68ab6267-e6fa-49b9-a3b4-d9c4bfd4b785',
MyModelStatus: MyModelStatus {
dataValues: [Object],
_previousDataValues: [Object],
uniqno: 1,
_changed: Set(0) {},
_options: [Object],
isNewRecord: false
},
firstName: 'User',
lastName: 'User',
email: 'abc@def.com',
phone: '12345678',
editorID: '8b8dfcfd-9c37-450e-a733-81d85421f285',
genderOfficial: 'male',
birthDate: '2024-07-25',
myModelStatusID: 'f2725742-0320-4249-8b57-9523a6003363'
},
_previousDataValues: {
id: '68ab6267-e6fa-49b9-a3b4-d9c4bfd4b785',
MyModelStatus: MyModelStatus {
dataValues: [Object],
_previousDataValues: [Object],
uniqno: 1,
_changed: Set(0) {},
_options: [Object],
isNewRecord: false
},
firstName: undefined,
lastName: undefined,
email: undefined,
phone: undefined,
editorID: undefined,
genderOfficial: undefined,
birthDate: undefined,
myModelStatusID: undefined
},
uniqno: 1,
_changed: Set(8) {
'firstName',
'lastName',
'email',
'phone',
'editorID',
'genderOfficial',
'birthDate',
'myModelStatusID'
},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: [ [Object] ],
includeNames: [ 'MyModelStatus' ],
includeMap: { MyModelStatus: [Object] },
includeValidated: true,
attributes: [ 'id' ],
raw: true
},
isNewRecord: false,
status: MyModelStatus {
dataValues: { name: 'new' },
_previousDataValues: { name: 'new' },
uniqno: 1,
_changed: Set(0) {},
_options: {
isNewRecord: false,
_schema: null,
_schemaDelimiter: '',
include: undefined,
includeNames: undefined,
includeMap: undefined,
includeValidated: true,
raw: true,
attributes: [Array]
},
isNewRecord: false
},
address: MyModelAddress {
dataValues: {
id: '5564d4b7-09c0-4633-b2c3-8b06376fad67',
streetNumber: '123',
street: 'bla',
postalCode: '123456',
city: 'anywhwere',
country: 'anywhere',
myModelID: '68ab6267-e6fa-49b9-a3b4-d9c4bfd4b785',
createdAt: 2024-07-15T10:10:30.533Z,
updatedAt: 2024-07-15T10:10:30.533Z
},
_previousDataValues: {
streetNumber: undefined,
street: undefined,
postalCode: undefined,
city: undefined,
country: undefined,
myModelID: undefined,
createdAt: undefined,
updatedAt: undefined
},
uniqno: 1,
_changed: Set(8) {
'streetNumber',
'street',
'postalCode',
'city',
'country',
'myModelID',
'createdAt',
'updatedAt'
},
_options: { isNewRecord: true, _schema: null, _schemaDelimiter: '' },
isNewRecord: true
}
}
Questions:
upsert()
not a function?You simply mixed up this.model
and model
. upsert
is the static and not an instance method:
const [instance, created] = await this.model.upsert({});