Str*_*e10 5 postgresql node.js sequelize.js
我遇到一个问题,即包含在我的查询中不起作用,但我不明白为什么。我有两个模型:问题和建议。
Question:
var Question = sequelize.define('Question', {
}, {
classMethods: {
associate: function (models) {
// associations can be defined here
Question.belongsTo(models.User, {
as: 'askerId',
foreignKey: 'askerId'
})
Question.belongsTo(models.User, {
as: 'winnerId',
foreignKey: 'winnerId'
})
Question.hasMany(models.Suggestion)
Question.hasMany(models.TagQuestion)
}
}
})
Run Code Online (Sandbox Code Playgroud)
建议:
var Suggestion = sequelize.define('Suggestion', {
text: DataTypes.STRING
}, {
classMethods: {
associate: function (models) {
// associations can be defined here
Suggestion.belongsTo(models.Question, {
foreignKey: 'questionId',
as: 'question'
})
Suggestion.belongsTo(models.User)
Suggestion.hasMany(models.Vote)
}
}
})
Run Code Online (Sandbox Code Playgroud)
我尝试的查询在这里:
Question.findAll({
include: [{
model: models.Suggestion
}]
})
Run Code Online (Sandbox Code Playgroud)
但我不断收到错误:SequelizeEagerLoadingError: Suggestion is not associated to Question!
为什么他们没有关联?它们在我的数据库中(基于我编写的迁移)。如果我目前的做法不正确,我应该如何建立我的协会?我看过其他人也有这个问题,但无法弄清楚为什么我的联想是错误的。
Sequelize 不再支持 classMethods。classMethods 和instanceMethods 被删除。
以前的:
const Model = sequelize.define('Model', {
...
}, {
classMethods: {
associate: function (model) {...}
},
instanceMethods: {
someMethod: function () { ...}
}
});
Run Code Online (Sandbox Code Playgroud)
新的:
const Model = sequelize.define('Model', {
...
});
Run Code Online (Sandbox Code Playgroud)
// 类方法
Model.associate = function (models) {
...associate the models`enter code here`
};
Run Code Online (Sandbox Code Playgroud)
参考:http://docs.sequelizejs.com/manual/tutorial/upgrade-to-v4.html#writing-changes
| 归档时间: |
|
| 查看次数: |
18674 次 |
| 最近记录: |