sequelize Mysql 表关联问题

222 阅读2分钟

由于需要将商品表和商品分类进行表关联,这里使用一对多的形式进行表关联,但第一次请求正常,第二次请求就会报错

SequelizeAssociationError: You have used the alias children in two separate associations. Aliased associations must have unique aliases.

实现表关联

// 一对多关联表
Dict_Data.hasMany(Goods,{
    foreignKey:'good_type',
    sourceKey:'dict_value',
    as:'children'
})

// 查询
const res = await Dict_Data.findAll({
    where:{dict_type:'goods_category'},
    include:[
        {
            model: Goods,
            as: 'children',
            where: {good_status: good_status}
        }
    ]
 })

数据结构如下:

{
    "code": 200,
    "message": "查询成功",
    "data": [
        {
            "id": 2,
            "dict_label": "生活用品",
            "dict_type": "goods_category",
            "dict_value": "2",
            "status": 1,
            "remark": null,
            "createdAt": "2022-12-29 01:17:23",
            "updatedAt": "2022-12-29 01:17:23",
            "children": [
                {
                    "id": 3,
                    "good_name": "222",
                    "good_price": "222.00",
                    "goods_num": 222,
                    "good_img": "[{"name":"创建队伍时序图.jpg","url":"http://localhost:8000/upload_d53382284208ebd7f14ae5898529a9e0.jpg"}]",
                    "good_type": "2",
                    "good_status": 1,
                    "good_intro": "<p>2222</p>",
                    "specification": "222",
                    "createdAt": "2022-12-29 01:30:43",
                    "updatedAt": "2022-12-29 01:30:43"
                },
                {
                    "id": 1,
                    "good_name": "111",
                    "good_price": "1111.00",
                    "goods_num": 111,
                    "good_img": "[{"name":"创建队伍流程图.jpg","url":"http://localhost:8000/upload_9a81426f1c8f5b29d70fb6c18a74acac.jpg"}]",
                    "good_type": "2",
                    "good_status": 1,
                    "good_intro": "<p>111</p>",
                    "specification": "111",
                    "createdAt": "2022-12-29 00:25:54",
                    "updatedAt": "2022-12-29 01:39:00"
                }
            ]
        },
        {
            "id": 1,
            "dict_label": "电子产品",
            "dict_type": "goods_category",
            "dict_value": "1",
            "status": 1,
            "remark": null,
            "createdAt": "2022-12-29 01:17:17",
            "updatedAt": "2022-12-29 01:17:17",
            "children": [
                {
                    "id": 2,
                    "good_name": "1111",
                    "good_price": "1111.00",
                    "goods_num": 1111,
                    "good_img": "[{"name":"创建队伍时序图.jpg","url":"http://localhost:8000/upload_5a35b14cf05b14af22883786c821ec30.jpg"}]",
                    "good_type": "1",
                    "good_status": 1,
                    "good_intro": "<p>1111</p>",
                    "specification": "1111",
                    "createdAt": "2022-12-29 00:26:04",
                    "updatedAt": "2022-12-29 00:26:04"
                }
            ]
        }
    ]
}

解决方法:创建关联前删除重复表名

// children 为自定义
if(Dict_Data.associations.children){
    delete Dict_Data.associations.children
}

子查询数据实现根据创建时间进行倒序排序

const res = await Dict_Data.findAll({
    where:{dict_type:'goods_category'},
    include:[
        {
            model: Goods,
            as: 'children',
            where: {good_status: good_status}
        }
    ],
    // 排序 层层递进
    order:[['children','createdAt','DESC']]
})