sequelize多对多:英雄表与装备表为多对多关系,自定义关联表,一个英雄拥有通用、顺风、逆风装备,如何更新关联表的字段?

谢谢啦!冒昧打扰一下各位高手高手高高手,这个问题可以帮我看一下怎么解决吗?我是小学生,回答我
英雄表(Hero)与装备表(Item)为多对多关系,自定义关联表(HeroItem),一个英雄拥有通用、顺风、逆风装备,
英雄表(Hero):

module.exports = (sequelize, Sequelize) => {

    const Hero = sequelize.define("hero", {
        id: {
            type: Sequelize.INTEGER,
            allowNull: false, 
            comment: '英雄ID',
            primaryKey: true, 
            autoIncrement: true,
        },

        name: {
            type: Sequelize.STRING,
            allowNull: false,
            comment: '英雄名称',
            validate: {
                notEmpty: {
                    msg: '请输入英雄名称'
                }
            }
        },       
    }, {
        tableName: 'hero',
        timestamps: false,
        freezeTableName: true 

    });
    return Hero;
}


装备表(Item)

module.exports = (sequelize, Sequelize) => {
    const Itme = sequelize.define('item', {
        id: {
            type: Sequelize.INTEGER,
            autoIncrement: true,
            comment: '主键',
            primaryKey: true, //设置主键
        },
        name: {
            type: Sequelize.STRING,
            allowNull: false,
            comment: '物品名称',
            validate: {
                notNull: {
                    msg: '缺少物品名称'
                }
            }
        },
    }, {
        timestamps: false,
        freezeTableName: true,
    })
    return Itme;
}


自定义关联表(HeroItem):

module.exports = (sequelize, Sequelize) => {
    const HeroItem = sequelize.define("HeroItem", {
        id: {
            type: Sequelize.INTEGER,
            allowNull: false,  
            comment: 'ID',
            primaryKey: true,  
            autoIncrement: true,
        },
        type: {
            type: Sequelize.INTEGER,
            allowNull: false,           
            comment: '0:顺逆风装备;1:顺风装备;2:逆风装备',
        },       
    }, {
        tableName: 'HeroItem',
        timestamps: false,
    });
    return HeroItem;
}

关联:

    /***
      自定义表
      英雄与装备多对多,
      英雄有两种出装,顺风、逆风
    ***/ 
    const Hero = require('./demo04/Hero')(sequelize, Sequelize); 
    const Item = require('./demo04/Item')(sequelize, Sequelize); 
    const HeroItem = require('./demo04/HeroItem')(sequelize, Sequelize);
    
    Hero.belongsToMany(Item, {
        through: 'HeroItem'
    });

    Item.belongsToMany(Hero, {
        through: 'HeroItem'
    });

问题一:创建新英雄时,怎么关联已存在装备(同时为顺风、逆风、通用),以下方法只关联了逆风装备,通用,顺风都关联不了,程序也没报错
问题二:如何一次性查询需要关联的装备,并把它们放到对应的位置(顺风、逆风、通用)


exports.createBoss = async (req, res, next) => {
    const t = await db.sequelize.transaction();

    try {
        const form = req.body    //{name:'云缨'}  
        // 通用装备
        let general = await item.findAll({
            where: {
                id: form.general //[1,2]
            }
        })

        // 顺风装备
        let advantage = await item.findAll({
            where: {
                id: form.advantage //[3,4]
            }
        })
        // 逆风装备
        let disadvantaged = await item.findAll({
            where: {
                id: form.disadvantaged //[5,6]
            }
        })

    const Hero = await hero.create(form)
    await Hero.setItems(general,{
            through: {
                type: 0
            },
            transaction: t 
        })
    await Hero.setItems(advantage,{
            through: {
                type: 1
            },
            transaction: t 
        })
    await Hero.setItems(disadvantaged,{
            through: {
                type: 2
            },
            transaction: t 
        })
        await t.commit();
        res.send({
            code: 0,
            mag: '创建英雄成功'
        });
    } catch (err) {
        await t.rollback();
        next(err)
    }
}


问题三:查询时我想的效果与实际效果

//实际效果
[ {
            "name": "云缨",
            "title": "燎原之心",           
            "items": [
                {
                    "id": 5,
                    "name": "噬神之书",
                    "HeroItem": {
                        "type": 1
                    }
                },
                {
                    "id": 6,
                    "name": "贤者之书",
                    "HeroItem": {
                        "type": 2
                    }
                }
            ]
        }]
//我想要的效果
[ {
            "name": "云缨",
            "title": "燎原之心",           
            "items": [
                {                    
                    "name": "噬神之书",
                     "type": 1
                },
                {                 
                    "name": "贤者之书",
                    "type": 2 
                }
            ]
        }]

最后,这样设计表是正确的吗?

阅读 1.1k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏