模型关联的问题
DATOULIN opened this issue · 3 comments
DATOULIN commented
请问,两个模型直接如何进行关联
DATOULIN commented
Tag.associate = models => {
Tag.belongsTo(models.article, {
as: 'article',
foreignKey: 'articleId',
targetKey: 'id',
constraints: false
})
}
models.article是什么,我没看到你有导入,你在哪做了关联,是在另一个文件里做了统一关联吗
我改成这种方式,也是不行
const Article = require("./article")
Tag.associate = models => {
Tag.belongsTo(Article, {
as: 'article',
foreignKey: 'articleId',
targetKey: 'id',
constraints: false
})
}
DATOULIN commented
fs.readdirSync(__dirname)
.filter(file => file !== 'index.js')
.forEach(file => {
const model = sequelize.import(path.join(__dirname, file))
db[model.name] = model
})
Object.keys(db).forEach(modelName => {
if (db[modelName].associate) {
db[modelName].associate(db)
}
})
这个代码我有点不理解
DATOULIN commented
明白了