gpt4 book ai didi

node.js - Sequelizer 连接多个 hasMany 关联

转载 作者:行者123 更新时间:2023-12-03 22:28:16 26 4
gpt4 key购买 nike

我正在关注官方的 Sequelizer 文档,但我无法让它与多个“hasMany”关联一起工作。

下面的代码呈现错误:Unhandled rejection Error: [object Object] is not associated to User!
这是如何正确完成的?

模型/用户.js

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
User.hasMany(models.Movie, models.Book)
}
}
});

return User;
};

路线/index.js
var models  = require('../models');
var express = require('express');
var router = express.Router();

router.get('/', function(req, res) {
models.User.findAll({
include: [ models.Task, models.Movie ]
}).then(function(users) {
res.render('index', {
title: 'Sequelize: Express Example',
users: users
});
});
});

module.exports = router;

最佳答案

发现问题,您必须单独定义它们(例如 User.hasMany 两次)

module.exports = function(sequelize, DataTypes) {
var User = sequelize.define("User", {
username: DataTypes.STRING
}, {
classMethods: {
associate: function(models) {
User.hasMany(models.Movie),
User.hasMany(models.Books)
}
}
});

return User;
};

关于node.js - Sequelizer 连接多个 hasMany 关联,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43447358/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com