user_group"."userUuid" ,尽管我没有在项目中引用字符串文字 userUuid 一次(通过不在-6ren">
gpt4 book ai didi

node.js - Sequelize 连接表错误 : DatabaseError [SequelizeDatabaseError]: column does not exist

转载 作者:行者123 更新时间:2023-12-03 22:24:26 25 4
gpt4 key购买 nike

我正在尝试运行以下代码块,由于某种原因,查询尝试将其插入标记为 的列中"users->user_group"."userUuid" ,尽管我没有在项目中引用字符串文字 userUuid 一次(通过不在代码库中搜索),但还要检查 pg-admin 中的列(使用 PostgreSQL),user_group 表中的两列都是 用户uuid group_uuid ,这两个列也都经过验证并正确填充。

const result = await group.findAll({
include: user,
});
postman 正文返回以下错误
"hint": "也许你的意思是引用列 "users->user_group.user_uuid"。",
我有 3 个模型用户、组和 user_group。这些关系已根据文档和无数其他文章和视频进行定义。
用户模型
module.exports = (sequelize, DataTypes) => {
const user = sequelize.define(
"user",
{
uuid: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
username: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
},
{
freezeTableName: true,
}
);

user.associate = (models) => {
user.belongsToMany(models.group, {
// as: "userUuid",
through: models.user_group,
foreignKey: "user_uuid",
});
};

return user;
};
团体模型
module.exports = (sequelize, DataTypes) => {
const group = sequelize.define(
"group",
{
uuid: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
title: {
type: DataTypes.STRING,
unique: true,
allowNull: false,
},
},
{
freezeTableName: true,
}
);

group.associate = (models) => {
group.belongsToMany(models.user, {
// as: "groupUuid",
through: models.user_group,
foreignKey: "group_uuid",
});
};

return group;
};
用户组模型
module.exports = (sequelize, DataTypes) => {
const user_group = sequelize.define(
"user_group",
{
uuid: {
type: DataTypes.STRING,
primaryKey: true,
allowNull: false,
unique: true,
},
user_uuid: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: "user",
key: "uuid",
},
},
group_uuid: {
type: DataTypes.STRING,
allowNull: false,
references: {
model: "group",
key: "uuid",
},
},
author: {
type: DataTypes.STRING,
unique: true,
allowNull: true,
},
},
{
freezeTableName: true,
}
);

user_group.associate = (models) => {
user_group.belongsTo(models.user, {
foreignKey: "user_uuid",
});
user_group.belongsTo(models.group, {
foreignKey: "group_uuid",
});
};

return user_group;
};
非常感谢任何帮助,谢谢!

最佳答案

您应该注明otherKey选项以及 foreignKeybelongsToMany为了在另一个模型上指示外键列,否则您最终会得到另一个键的默认名称,见下文:

The name of the foreign key in the join table (representing the target model) or an object representing the type definition for the other column (see Sequelize.define for syntax). When using an object, you can add a name property to set the name of the column. Defaults to the name of target + primary key of target (your case: user+uuid)

group.belongsToMany(models.user, {
// as: "groupUuid",
through: models.user_group,
foreignKey: "group_uuid",
otherKey: "user_uuid"
});

关于node.js - Sequelize 连接表错误 : DatabaseError [SequelizeDatabaseError]: column does not exist,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/66976200/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com