【问题标题】:Sequelize error with join table: DatabaseError [SequelizeDatabaseError]: column does not exist连接表的 Sequelize 错误:DatabaseError [SequelizeDatabaseError]:列不存在
【发布时间】:2021-04-08 17:50:41
【问题描述】:

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

const result = await group.findAll({
  include: user,
});

邮递员正文返回以下错误 "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;
};

非常感谢任何帮助,谢谢!

【问题讨论】:

    标签: node.js postgresql sequelize.js


    【解决方案1】:

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

    连接表中外键的名称(表示目标模型)或表示另一列的类型定义的对象(语法参见 Sequelize.define)。使用对象时,可以添加 name 属性来设置列的名称。 默认为目标名称+目标主键(您的情况:user+uuid

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

    【讨论】:

    • 是的,结果证明 sequlzie 搞乱了从表名到列名的几乎所有内容的命名顺序(仅限外键)。必须将所有内容从 _ 更改为 camelCase,因为显然他们为那里的模型设置的下划线对象(下划线:true)非常有问题,
    【解决方案2】:
    const result = await group.findAll({
      include: {user},
    });
    

    你应该像这样创建。因为你错过了这个{}。

    【讨论】:

      猜你喜欢
      • 2022-11-10
      • 2021-10-26
      • 2017-02-19
      • 2020-06-06
      • 1970-01-01
      • 2021-04-04
      • 2018-04-29
      • 1970-01-01
      • 2016-08-27
      相关资源
      最近更新 更多