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

标签 node.js postgresql sequelize.js

我正在尝试运行以下代码块,由于某种原因,查询尝试将其插入标记为 的列中"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/

相关文章:

node.js - 如何在使用 docker-compose 和非 root 用户时将 node_modules 保留在容器中?

postgresql - osm_ways 表中的长度与 length_m

postgresql 重复键违反唯一约束

validation - Sequelize 验证方法

node.js - 获取DialogSet.add() : Invalid dialog being added when testing

node.js - 使用 AWS 无服务器和 NodeJS 在接收器 lambda 上未从 SQS 接收所有消息

javascript - findOrCreate 包含 - sequelize.js

sql-server - Feathers 和 Sequelize SQL Server 错误

node.js - Telnet:Windows 7 上的本地主机无法连接到本地主机

索引字段上的 Postgresql 9.2 时间戳排序不会在一天内按小时部分排序