gpt4 book ai didi

javascript - Node.JS & Sequelize 定义关系

转载 作者:行者123 更新时间:2023-12-03 22:40:36 25 4
gpt4 key购买 nike

有 2 种型号,客户和订单
在模型订单中定义

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

....

如何定义这些没有错误?

感谢帮助

最佳答案

试试这个

    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'
});

关于javascript - Node.JS & Sequelize 定义关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48008448/

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