Node.js Sequelize使用带有自定义where子句的upsert函数
我使用Node.js Sequelize使用带有自定义where子句的upsert函数,node.js,postgresql,sequelize.js,Node.js,Postgresql,Sequelize.js,我使用Sequelize与我的postgres数据库通信。我想使用upsert功能,但要使用customwhere子句。例如,我有一个志愿者位置模型 module.exports = function (sequelize, DataTypes) { var volunteer_location = sequelize.define('volunteer_location', { id: { type: Sequelize.INTEGER,
Sequelize
与我的postgres
数据库通信。我想使用upsert
功能,但要使用customwhere子句。例如,我有一个志愿者位置模型
module.exports = function (sequelize, DataTypes) {
var volunteer_location = sequelize.define('volunteer_location', {
id: {
type: Sequelize.INTEGER,
allowNull: false,
autoIncrement: true,
primaryKey: true
},
deviceId: {
allowNull: false,
type: Sequelize.STRING
},
latitude: {
allowNull: false,
type: Sequelize.DECIMAL(18, 14)
},
longitude: {
allowNull: false,
type: Sequelize.DECIMAL(18, 14)
},
city: {
allowNull: false,
type: Sequelize.STRING(90)
},
state: {
type: Sequelize.STRING(90)
},
postalCode: {
type: Sequelize.STRING(16)
},
country: {
allowNull: false,
type: Sequelize.STRING(70)
}
}, {
instanceMethods: {
insertOrUpdate: function (requestBody, res) {
volunteer_location.upsert(requestBody).then(function () {
res.sendStatus(200);
});
}
}
});
return volunteer_location;
};
在insertOrUpdate
方法中,我尝试通过给出类似json的
location = {
deviceId: req.body.deviceId,
latitude: req.body.latitude,
longitude: req.body.longitude,
city: req.body.city,
state: req.body.state,
postalCode: req.body.postalCode,
country: req.body.country,
volunteer: req.decoded.id
};
其中,志愿者是表用户中的外键。
现在执行官说:
创建或替换函数pg_temp.sequelize_upsert()返回整数
当$func$开始插入“志愿者位置”时,会返回一些值
1.唯一的\u违规时出现异常,然后更新“志愿者\u位置”
设置一些值,其中((“id”为NULL,“志愿者”=1));
返回2;结束$func$语言plpgsql;从中选择*
pg_temp.suquelize_upsert()
id
和志愿者
是一组主键。我想通过只检查志愿者
值来更改更新
中的WHERE
子句,因为我不知道id
的值
这可能吗?或者我必须使用
findOne(...).then(function () {
//update
});
编辑
我使用属性unique:true
设置志愿者
,并将where子句替换为where((“id”为NULL,“志愿者”=1)或“志愿者”=1)代码>,表示更新有效。但我不知道这是否很有效。如果有人知道更好的解决方案,请告诉我