有两种型号,Customer&订购 在模型Order define
中Order.belongsTo(Customer {foreignKey: 'customer_id', as: 'customer'});
在模型客户定义
中Customer.hasMany(Order, {foreignKey: 'customer_id', as: 'orders'});
出现此错误
Error: customer.hasMany called with something that's not a subclass of Sequelize.Model
...
如何定义这些没有错误?
感谢您的帮助
答案 0 :(得分:0)
我的猜测是你定义一个模型及其与同一文件的关联。我建议你在模型文件的单独文件中定义关联。 E.g:
order.js
:定义Order
模型customer.js
:定义Customer
模型association.js
:定义Order
和Customer
答案 1 :(得分:0)
试试这个
var config = require('../config/environment');
var Sequelize = require('sequelize');
var db = {
Sequelize,
sequelize: new Sequelize(config.mysql.uri, config.mysql.options)
};
db.Customer = db.sequelize.import(modal_path); //your schema
db.Order = db.sequelize.import(modal_path); //your schema
db.Customer.hasMany(db.Order, {
foreignKey: 'customer_id'
});
db.Order.belongsTo(db.Customer, {
foreignKey: 'customer_id',
targetKey: 'Id'
});