Node.JS和Sequelize定义关系
有两种型号,客户和订单型号订单定义
Order.belongsTo(Customer {foreignKey: 'customer_id', as: 'customer'});
在模型Customer定义中
Customer.hasMany(Order, {foreignKey: 'customer_id', as: 'orders'});
得到了这个错误
Error: customer.hasMany called with something that's not a subclass of Sequelize.Model
....
如何定义这些没有错误?
感谢帮助
回答如下:试试这个
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'
});