Sequelize Association query problem

the problem encountered at present is that the problem needed after associating a query through sequelize is that the result of the associative query is fine for the first time, and an error will be reported after another query.
1, two associated models
(1) menu model

"use strict";

module.exports = app => {
    const { STRING, INTEGER, DATE } = app.Sequelize;
    let table_name = "qt_admin_menu";

    const AdminMenu = app.model.define(table_name, {
        menu_id: { 
            type: "VARCHAR(32)",
            allowNull: false,
            defaultValue: "",
            primaryKey: true 
        },
        parent_id: { 
            type: "VARCHAR(32)",
            allowNull: false,
            defaultValue: "0",
            primaryKey: false 
        },
        pk: { 
            type: "VARCHAR(32)",
            allowNull: false,
            defaultValue: "",
            primaryKey: false 
        },
         menu_type: {
            type: "TINYINT(3) UNSIGNED",
            allowNull: true,
            defaultValue: "1",
            primaryKey: false
        },
        top_menu: {
            type: "INT(1) UNSIGNED",
            allowNull: true,
            defaultValue: "2",
            primaryKey: false
        },
        status: {
            type: "TINYINT(3) UNSIGNED",
            allowNull: true,
            defaultValue: "1",
            primaryKey: false
        },
        list_order: {
            type: "TINYINT(5) UNSIGNED",
            allowNull: true,
            defaultValue: "1",
            primaryKey: false
        },
        menu_app: {
            type: "VARCHAR(15)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        controller: {
            type: "VARCHAR(30)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        action: {
            type: "VARCHAR(30)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        url_method: {
            type: "VARCHAR(10)",
            allowNull: true,
            defaultValue: "get",
            primaryKey: false
        },
        menu_url: {
            type: "VARCHAR(200)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        param: {
            type: "VARCHAR(50)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        menu_name: {
            type: "VARCHAR(30)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        icon: {
            type: "VARCHAR(20)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        remark: {
            type: "VARCHAR(255)",
            allowNull: true,
            defaultValue: "",
            primaryKey: false
        },
        isdelete: {
            type: "INT(1)",
            allowNull: true,
            defaultValue: "2",
            primaryKey: false
        }
    });
    // AdminMenu.removeAttribute("id");
    return AdminMenu;
};

(2) role menu associated table model

"use strict";

module.exports = app => {
    const { STRING, INTEGER, DATE } = app.Sequelize;
    let table_name = "qt_admin_role_menu";

    const AdminRoleMenu = app.model.define(table_name, {
        id: {
            type: "INT(11)",
            allowNull: false,
            defaultValue: null,
            primaryKey: true,
            autoIncrement: true
        },
        role_id: {
            type: "INT(11)",
            allowNull: false,
            primaryKey: false 
        },
        menu_id: {
            type: "VARCHAR(32)",
            allowNull: false,
            defaultValue: "",
            primaryKey: false 
        }
    });

    return AdminRoleMenu;
};

2, query in service

"use strict";
const Service = require("egg").Service;

class MenuService extends Service {
    // 
    constructor(ctx) {
        // `this.ctx`
        super(ctx); 
        //  this.ctx  ctx 
        //  this.app  app 
        this.table_name = "qt_admin_menu";
        // model
        this.adminMenuModel = ctx.model.AdminMenu;
        this.adminRoleMenuModel = ctx.model.AdminRoleMenu;
        //  hasMany 
        this.adminMenuModel.hasMany(this.adminRoleMenuModel, {
            as: "u",
            through: null,
            foreignKey: "menu_id"
        })
        // this.adminRoleMenuModel.belongsToMany(this.adminMenuModel, {
        //     // as: "adminMenuModel",
        //     foreignKey: "menu_id"
        // });
    }
    async getRoleMenu(rid){
        // [this.app.Sequelize.fn("if", "adminRoleMenuModel.menu_id is null", "false", "true"), "selected"]
        const result = await this.adminMenuModel.findAll({
            raw: true,
            attributes: ["menu_id", "parent_id", "menu_name", "icon", [this.app.Sequelize.col("u.id"), "selected"]],
            include: [{
                attributes: [],
                model: this.adminRoleMenuModel,
                as: "u",
                where: {
                    role_id: rid,
                },
                required: false
            }],
            order: [
                ["top_menu", "ASC"],
                ["list_order", "ASC"]
            ]
        })
        // .then(rows => rows && rows.map(r => r.toJSON()));
        // console.log(result);
        //const result = await this.app.mysql.query("select a.*, b.menu_id as selectedex, if(b.menu_id is null,"false","true") as selected from qt_admin_menu a left join qt_admin_role_menu b on a.menu_id = b.menu_id and b.role_id = ? ORDER BY a.top_menu, a.list_order", [rid]);
        return result;
    }

}
module.exports = MenuService;

3. Query result
after modifying the file, execute the request, the query result is normal, and the printing SQL is also normal

SELECT `qt_admin_menu`.`menu_id`, `qt_admin_menu`.`parent_id`, `qt_admin_menu`.`menu_name`, `qt_admin_menu`.`icon`, `u`.`id` AS `selected` FROM `qt_admin_menu` AS `qt_admin_menu` LEFT OUTER JOIN `qt_admin_role_menu` AS `u` ON `qt_admin_menu`.`menu_id` = `u`.`menu_id` AND `u`.`role_id` = "3" ORDER BY `qt_admin_menu`.`top_menu` ASC, `qt_admin_menu`.`list_order` ASC;

result data:

 { menu_id: "ef535c8a938665d1d0641a4e86ee83ed",
    parent_id: "a8e66f7be77c9bf71a2511331ac6ba32",
    menu_name: "",
    icon: null,
    selected: null }

but execute it again and you will get an error
You have used the alias u in two separate associations. Aliased associations must have unique aliases.

the corresponding problem can not be found on the Internet. I hope the god can help solve it. Thank you very much!

Jun.30,2022

reference for related queries in service

const Categories = Product.hasMany(Tag, { as: 'categories' });

Product.create({
  id: 1,
  title: 'Chair',
  categories: [
    { id: 1, name: 'Alpha' },
    { id: 2, name: 'Beta' }
  ]
}, {
  include: [{
    model: Categories,
    as: 'categories'
  }]
})

make sure the as is unique. https://github.com/sequelize/...


Thank you for the help of the bosses. The basic guidance is the cause of the problem. Be sure to specify both singular and plural forms when assigning aliases, or you will make a mistake

.
//  hasMany 
        this.adminMenuModel.hasMany(this.adminRoleMenuModel, {
            as: { 
                singular: 'adminRoleMenuModel', 
                plural: 'adminRoleMenuModels'
            },
            foreignKey: 'menu_id'
        })

I feel that the egg designed by orm hurts a lot

.
MySQL Query : SELECT * FROM `codeshelper`.`v9_news` WHERE status=99 AND catid='6' ORDER BY rand() LIMIT 5
MySQL Error : Disk full (/tmp/#sql-temptable-64f5-1e4603d-43ed5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
MySQL Errno : 1021
Message : Disk full (/tmp/#sql-temptable-64f5-1e4603d-43ed5.MAI); waiting for someone to free some space... (errno: 28 "No space left on device")
Need Help?