node js中的多对多关联 [英] many to many association in node js

查看:78
本文介绍了node js中的多对多关联的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这里是我的代码链接.我在 api-routes-index.js 中的 index.js 中遇到错误.获取错误未定义的地图函数

推荐答案

sequilize.js 中的多对多关联这将有助于您解决问题:

Many to many associations in sequilize.js This will be helpful for you to solve the issue:

一个产品有多个订单

一个订单有多个产品

由于多对多,我们有连接表orderproduct"其中包含 product_id 和 order_id.

As a result of many to many we have junction table "orderproduct" which contains product_id and order_id.

   //  In product model
    product.belongsToMany(order, {          
                through: 'orderproduct',
                foreignKey: 'product_id'
            });

   // In order model
    order.belongsToMany(product, {          
                through: 'orderproduct',
                foreignKey: 'order_id'
            });

   // In oderproduct model 
        orderproduct.belongsTo(product, {
            foreignKey: { name: 'product_id', allowNull: false }
        });
        
        orderproduct.belongsTo (order, {
            foreignKey: { name: 'order_id', allowNull: false }
        });
    }

这篇关于node js中的多对多关联的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆