Sequelize upsert 总是 creating/inserting post 请求的新条目,而不是更新匹配用户名的数据。 MySQL 数据库
Sequelize upsert is always creating/inserting a new entry on post request, instead of updating data on matching username. MySQL database
我正在尝试针对特定数量的用户进行调查。
当用户第一次提交他们的答案时,我想在数据库中创建一个新条目,这部分工作正常。如果同一用户想重新参加调查,我想更新他们的答案,而不是为同一用户创建新条目。
我正在尝试使用 sequelize upsert,但每次我发出新的 post 请求时,它都会为同一个“testuser”创建新条目。
None 在网上找到的答案似乎有效。对我可能做错的任何帮助表示赞赏。
// parts of the code that might not be relevant to the problem have been omitted
const express = require('express');
const bodyParser = require('body-parser');
const Sequelize = require('sequelize');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const sequelize = new Sequelize({
database: 'database',
username: 'user',
password: 'password',
dialect: 'mysql',
});
sequelize
.authenticate()
.then(() => console.log('Connection has been established successfully.'))
.catch(err => console.log('error: ', err));
const SurveyResults = sequelize.define('results', {
name: {
type: Sequelize.STRING,
primaryKey: true,
unique: true,
},
results: {
type: Sequelize.JSON,
},
});
SurveyResults.sync()
.then(() => console.log('Survey results table created'))
.catch(err => console.log(err));
app.post('/dashboard', (req, res) => {
let { username, surveyData } = req.body;
SurveyResults.upsert(
{
name: username,
results: surveyData,
},
{ name: username }
)
.then(data => console.log(data))
.catch(err => console.log(err));
});
const PORT = process.env.PORT || 5050;
app.listen(PORT, () => console.log('Server is running on port ' + PORT));
Upsert on mysql 仅当在 not 中给出相同的主键时才有效,这将创建一个新的。
您可以参考:https://sequelize.org/master/class/lib/model.js~Model.html#static-method-upsert
Pk 不必是唯一的,它是多余的。
如果用户名不是主键,则在该列上创建另一个唯一索引,如果找不到相同的用户名,upsert 将像创建一样工作,如果相同的用户名已存在于 table 中,则更新。
我正在尝试针对特定数量的用户进行调查。 当用户第一次提交他们的答案时,我想在数据库中创建一个新条目,这部分工作正常。如果同一用户想重新参加调查,我想更新他们的答案,而不是为同一用户创建新条目。
我正在尝试使用 sequelize upsert,但每次我发出新的 post 请求时,它都会为同一个“testuser”创建新条目。
None 在网上找到的答案似乎有效。对我可能做错的任何帮助表示赞赏。
// parts of the code that might not be relevant to the problem have been omitted
const express = require('express');
const bodyParser = require('body-parser');
const Sequelize = require('sequelize');
const app = express();
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({ extended: true }));
const sequelize = new Sequelize({
database: 'database',
username: 'user',
password: 'password',
dialect: 'mysql',
});
sequelize
.authenticate()
.then(() => console.log('Connection has been established successfully.'))
.catch(err => console.log('error: ', err));
const SurveyResults = sequelize.define('results', {
name: {
type: Sequelize.STRING,
primaryKey: true,
unique: true,
},
results: {
type: Sequelize.JSON,
},
});
SurveyResults.sync()
.then(() => console.log('Survey results table created'))
.catch(err => console.log(err));
app.post('/dashboard', (req, res) => {
let { username, surveyData } = req.body;
SurveyResults.upsert(
{
name: username,
results: surveyData,
},
{ name: username }
)
.then(data => console.log(data))
.catch(err => console.log(err));
});
const PORT = process.env.PORT || 5050;
app.listen(PORT, () => console.log('Server is running on port ' + PORT));
Upsert on mysql 仅当在 not 中给出相同的主键时才有效,这将创建一个新的。
您可以参考:https://sequelize.org/master/class/lib/model.js~Model.html#static-method-upsert
Pk 不必是唯一的,它是多余的。
如果用户名不是主键,则在该列上创建另一个唯一索引,如果找不到相同的用户名,upsert 将像创建一样工作,如果相同的用户名已存在于 table 中,则更新。