######### 调查模型#########
module.exports = function(sequelize, DataTypes) {
const Survey = sequelize.define("Survey", {
survey_id: {
autoIncrement: true,
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
primaryKey: true,
},
required: {
type: DataTypes.INTEGER
},
title: {
type: DataTypes.STRING
},
description: {
type: DataTypes.STRING
},
status: {
type: DataTypes.INTEGER
},
endDate: {
type: DataTypes.DATE
},
userid: {
type: DataTypes.INTEGER
}
},{
tableName: 'survey',
hooks,
});
Survey.sync({ alter: true })
Survey.associate = function(models) {
Survey.belongsTo(models.User, {foreignKey: 'userid'});
Survey.hasMany(models.Question);
};
return Survey }
################ 提问模型#######
module.exports = function (sequelize, DataTypes) {
const Question = sequelize.define("Question", {
question_id: {
autoIncrement: true,
type: DataTypes.INTEGER.UNSIGNED,
allowNull: false,
primaryKey: true,
},
question_type: {
type: DataTypes.INTEGER
},
question_title: {
type: DataTypes.STRING
},
question_required: {
type: DataTypes.INTEGER
},
survey_id: {
type: DataTypes.INTEGER
}
}, {
tableName: 'question',
hooks,
});
Question.sync({alter: true})
Question.associate = (models) => {
Question.belongsTo(models.Survey, {foreignKey: 'survey_id'});
// Question.hasMany(models.QuestionOption, {foreignKey: 'question_id'});
};
return Question }
######### 控制器文件########
Survey.findAll({
where: {
userid: userId
},
include: {
model: Question,
}
}).then(data => {
res.status(200).send(formatResponse(false, 'Survey list', data));
}).catch(error => {
res.status(500).send(formatResponse(true, error.message || "Error in Fetching Data", {}));
})
我是nodejs的初学者。我使用postgres作为数据库。与谷歌和其他博客的帮助下,使登录,注册API。但当我去的进步,然后这个错误显示。不明白为什么会出现这个错误。帮我摆脱这一切。我尝试了许多其他的解决办法,但同样的错误来
1条答案
按热度按时间bhmjp9jg1#
我认为你必须提供一个外键,将调查与问题相关联。在这种情况下,外键将是
survey_id
。你必须像下面这样修改你的调查模型。