我使用 Sequelize 将数据库链接到我的应用程序。但我最近遇到了上面的bug。到处都说这是数据库错误。由于两个表具有相同的主键。在 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);
});
});
最佳答案
两个表都包含字段 id
,因此在 COUNT 函数中 SQL 不知道要使用哪个 id
。尝试在 sequelize.col("id")
中指定 ID。例如sequelize.col("Employees.id")
。
关于javascript - Sequelize错误: column reference "id" is ambiguous.无法在文档中找到答案,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66153760/