首页 > 解决方案 > 您如何正确更改续集中的模型?

问题描述

我有以下两个模型,并试图将 Report 模型上的 userId 属性更改为不必是唯一的。models.Report.create({ userId: 1 })当我将 unique 更改为 false 或完全删除该属性时,在尝试使用现有 userId 创建报告时(当 1 是现有用户的 id 时使用)时,我仍然收到唯一验证错误。我也尝试过进行迁移并运行它无济于事。

我在这里错过了什么吗?一直坚持这一点,因此非常感谢任何输入。谢谢你。

报告.js


module.exports = (sequelize, DataTypes) => {
  const Report = sequelize.define(
    'Report',
      userId: {
        allowNull: false,
        type: Sequelize.INTEGER,       
        unique: true,
      },
  );

  Report.associate = function(models) {
    Report.belongsTo(models.UserInfo, { foreignKey: 'userId' });
  };
  return Report;
};

用户信息.js

module.exports = (sequelize, DataTypes) => {
  const UserInfo = sequelize.define(
    'UserInfo',
    {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
  );

  UserInfo.associate = function associate(models) {
    ApplicantInfo.hasMany(models.Report, { foreignKey: 'applicantId' });
  };

  return ApplicantInfo;
};

标签: javascriptpostgresqlsequelize.js

解决方案


推荐阅读