【问题标题】:Sequelize fetch include data based on alias where conditionSequelize fetch 包含基于别名 where 条件的数据
【发布时间】:2021-07-23 15:06:18
【问题描述】:

我有两张桌子EmployeeDepartment

Department

    const Department = Sequelize.define(
        "Department",
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            name: {
                type: DataTypes.STRING,
                allowNull: false,
            },
        },
        {
            underscored: true,
            timestamps: true,
            paranoid: true,
            modelName: "Department",
            tableName: "departments",
        },
    );

    Department.associate = function (models) {
        // associations can be defined here
        models.Department.hasMany(models.Employee, {
            foreignKey: "department_id",
            as: "employees",
        });
    };

    return Department;
};

Employee

    const Employee = Sequelize.define(
        "Employee",
        {
            id: {
                type: DataTypes.INTEGER,
                autoIncrement: true,
                primaryKey: true,
            },
            name: {
                type: DataTypes.STRING,
                unique: true,
                allowNull: false,
            },
            status: {
                type: DataTypes.STRING,
                defaultValue: "active",
            },
            departmentId: {
                type: DataTypes.INTEGER,
            },
        },
        {
            underscored: true,
            timestamps: true,
            modelName: "Employee",
            tableName: "employees",
        },
    );

    Employee.associate = function (models) {
        models.Employee.belongsTo(models.Department, {
            foreignKey: "department_id",
            as: "department",
        });
    };

    return Employee;
};

现在我必须获取员工列表并放置一个过滤器 department_id = 1

const { departmentId } = req.body;

const employees = await Employee.findAll({
    include: [
        {
            model: Department,
            where: {
                id: departmentId,
            },
        },
    ],
});

我遇到了问题。部门由关联“部门”映射 无法获取数据。

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

    我在 sequelize docs 上找到了答案

    
    const employees = await Employee.findAll({
        include: [
            {
                association: "department", // this is the place to change 
                where: {
                    id: departmentId,
                },
            },
        ],
    });
    

    学习:

    1. 我们将无法将关联和模型放在一起。
    2. 如果没有关联,我们将能够使用模型。
    3. 如果有关联,我们将能够使用关联。

    参考资料: https://sequelize.org/master/manual/eager-loading.html#:~:text=You%20can%20also%20include%20by%20alias%20name%20by%20specifying%20a%20string%20that%20matches%20the%20association%20alias

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-02-09
      • 1970-01-01
      • 1970-01-01
      • 2019-11-21
      • 1970-01-01
      • 2017-08-27
      • 2019-12-30
      • 2019-10-24
      相关资源
      最近更新 更多