【问题标题】:Sequelize error: column reference "id" is ambiguous. Can't find answer in documentation续集错误:列引用“id”不明确。在文档中找不到答案
【发布时间】:2021-05-15 03:16:24
【问题描述】:

我使用 Sequelize 将数据库链接到我的应用程序。但是我最近遇到了上述错误。它到处都说这是一个数据库错误。由于两个表具有相同的主键。在 SQL 中,这是通过别名解决的。但是在 Sequelize 的文档中,我还没有找到解决这个问题的方法。我该怎么办?

    const { DataTypes } = require("sequelize");
const db = require("../config/database");

const Office = db.define(
    "Office",
    {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        address: DataTypes.STRING(45),
    },
    {
        // Other model options go here
        tableName: "office",
        timestamps: false,
    }
);

const Employees = db.define(
    "Employees",
    {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        name: DataTypes.STRING(45),
        job: DataTypes.STRING(45),
        reg_date: DataTypes.DATEONLY,
        salary: DataTypes.DECIMAL(10, 2),
        weekend: DataTypes.INTEGER,
        office_id: { type: DataTypes.INTEGER, allowNull: false },
    },
    {
        // Other model options go here
        tableName: "employees",
        timestamps: false,
    }
);

Employees.belongsTo(Office, { foreignKey: "office_id" }); // Foreign key

const Developer = db.define(
    "Developer",
    {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        role: DataTypes.STRING(45),
        level: DataTypes.STRING(45),
        project_count: DataTypes.INTEGER,
    },
    {
        // Other model options go here
        tableName: "developer",
        timestamps: false,
    }
);

// Developer.belongsTo(Employees);
Employees.hasOne(Developer, { foreignKey: "id", targetKey: "id" }); // Foreign key

const Clients = db.define(
    "Clients",
    {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        name: DataTypes.STRING(45),
        total_sum: DataTypes.DECIMAL(20, 2),
    },
    {
        // Other model options go here
        tableName: "clients",
        timestamps: false,
    }
);

const Projects = db.define(
    "Projects",
    {
        id: {
            type: DataTypes.INTEGER,
            allowNull: false,
            primaryKey: true,
            autoIncrement: true,
        },
        price: DataTypes.DECIMAL(15, 2),
        started: DataTypes.DATEONLY,
        ended: DataTypes.DATEONLY,
        teamlead_id: { type: DataTypes.INTEGER, allowNull: false },
        designer_id: { type: DataTypes.INTEGER, allowNull: false },
        programmer_id: { type: DataTypes.INTEGER, allowNull: false },
        dbarch_id: { type: DataTypes.INTEGER, allowNull: false },
        client_id: { type: DataTypes.INTEGER, allowNull: false },
    },
    {
        // Other model options go here
        tableName: "projects",
        timestamps: false,
    }
);

Developer.hasOne(Projects, { foreignKey: "teamlead_id" }); // Foreign key
Developer.hasOne(Projects, { foreignKey: "designer_id" }); // Foreign key
Developer.hasOne(Projects, { foreignKey: "programmer_id" }); // Foreign key
Developer.hasOne(Projects, { foreignKey: "dbarch_id" }); // Foreign key
Clients.hasOne(Projects, { foreignKey: "client_id" }); // Foreign key

module.exports = { Clients, Developer, Employees, Office, Projects };

并表达要求:

app.get("/office_dev_workers_spec_count", (req, res) => {
    Employees.findAll({
        include: {
            model: Developer,
            where: {
                "$Developer.role$": req.query.dev,
                "$Developer.level$": req.query.lvl,
                "$Employees.office_id$": req.query.office,
            },
            attributes: [
                [sequelize.fn("COUNT", sequelize.col("id")), "n_devEmployees"],
            ],
        },
    }).then((result) => {
        res.send(result);
    });
});

【问题讨论】:

    标签: javascript node.js postgresql express sequelize.js


    【解决方案1】:

    您的两个表都包含字段 id,因此在 COUNT 函数中 SQL 不知道要使用哪个 id。尝试在sequelize.col("id") 中指定 ID。例如sequelize.col("Employees.id")

    【讨论】:

    • 不幸的是,这导致:'UnhandledPromiseRejectionWarning:SequelizeDatabaseError:列“Employees.id”必须出现在 GROUP BY 子句中或用于聚合函数中'
    • 对。你能指定你的 SQL 查询吗?代码中的那个和sequelize code不同。
    • 我指定了正确的查询。就在我发送请求时,我得到了这个输出。 pastebin.com/SbiA1Wnb
    • 评论中的查询中没有COUNT。但是您的续集查询中有COUNT。为什么添加它?
    • 不要看评论的查询。这些是我的应用程序第一次迭代的剩余部分。我第一次忘记添加COUNT,所以我在第二次迭代中添加了它,我只使用了 Sequelize 内置模型功能。
    猜你喜欢
    • 2018-01-26
    • 1970-01-01
    • 2015-01-07
    • 2018-07-02
    • 2020-07-18
    • 2012-04-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多