Sequelize错误:无法找到未定义的属性“startYear”

时间:2018-06-16 16:30:41

标签: node.js express sequelize.js sequelize-cli

我正在使用sequelize cli并为每个生成的模型获取每个迁移文件,我正在尝试使用单个迁移文件生成所有表,因为它们彼此依赖,但我不断收到此错误

无法找到未定义的属性startYear

另外,我需要一个关于如何使用sequelize cli来改变续集模型的教程。

"use strict";
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface
      .createTable("Users", {
        id: {
          allowNull: false,
          autoIncrement: true,
          primaryKey: true,
          type: Sequelize.INTEGER
        },
        googleId: {
          allowNull: true,
          type: Sequelize.STRING
        },
        facebookId: {
          allowNull: true,
          type: Sequelize.STRING
        },
        fullName: {
          type: Sequelize.STRING,
          allowNull: false
        },
        email: {
          type: Sequelize.STRING,
          allowNull: false,
          unique: true,
          validate: {
            isEmail: true
          }
        },
        password: {
          type: Sequelize.STRING,
          allowNull: false,
          validate: {
            isAlphanumeric: true,
            notEmpty: true
          }
        },
        isVerified: {
          type: Sequelize.BOOLEAN,
          allowNull: false,
          defaultValue: false
        },
        createdAt: {
          allowNull: false,
          type: Sequelize.DATE
        },
        updatedAt: {
          allowNull: false,
          type: Sequelize.DATE
        }
      })
      .then(function() {
        return queryInterface
          .createTable("DesignationMasters", {
            id: {
              allowNull: false,
              autoIncrement: true,
              primaryKey: true,
              type: Sequelize.INTEGER
            },
            designationName: {
              type: Sequelize.STRING,
              allowNull: false,
              unique: true
            },
            createdAt: {
              allowNull: false,
              type: Sequelize.DATE
            },
            updatedAt: {
              allowNull: false,
              type: Sequelize.DATE
            }
          })
          .then(function() {
            return queryInterface
              .createTable("companyDetails", {
                id: {
                  allowNull: false,
                  autoIncrement: true,
                  primaryKey: true,
                  type: Sequelize.INTEGER
                },
                companyName: {
                  type: Sequelize.STRING,
                  allowNull: true,
                  defaultValue: null
                },
                userId: {
                  type: Sequelize.INTEGER,
                  allowNull: false,
                  references: {
                    model: "Users",
                    key: "id"
                  }
                },
                designationId: {
                  type: Sequelize.INTEGER,
                  allowNull: false,
                  references: {
                    model: "DesignationMasters",
                    key: "id"
                  }
                },
                startYear: {
                  type: Sequelize.INTEGER,
                  validate: {
                    isNumeric: true,
                    len: [4, 4]
                  },
                  defaultValue: null
                },
                endYear: {
                  type: Sequelize.INTEGER,
                  validate: {
                    isNumeric: true,
                    len: [4, 4],
                    min: this.startYear
                  },
                  defaultValue: null
                },
                isCurrentWorkplace: {
                  type: Sequelize.BOOLEAN,
                  defaultValue: false
                },
                createdAt: {
                  allowNull: false,
                  type: Sequelize.DATE
                },
                updatedAt: {
                  allowNull: false,
                  type: Sequelize.DATE
                }
              })
              .then(function() {
                return queryInterface
                  .createTable("InterestMasters", {
                    id: {
                      allowNull: false,
                      autoIncrement: true,
                      primaryKey: true,
                      type: Sequelize.INTEGER
                    },
                    interestValue: {
                      allowNull: false,
                      type: Sequelize.STRING
                    },
                    createdAt: {
                      allowNull: false,
                      type: Sequelize.DATE
                    },
                    updatedAt: {
                      allowNull: false,
                      type: Sequelize.DATE
                    }
                  })
                  .then(function() {
                    return queryInterface.createTable("UserInterests", {
                      id: {
                        allowNull: false,
                        autoIncrement: true,
                        primaryKey: true,
                        type: Sequelize.INTEGER
                      },
                      userId: {
                        type: Sequelize.INTEGER,
                        references: {
                          model: "Users",
                          key: "id"
                        },
                        allowNull: false
                      },
                      interestId: {
                        type: Sequelize.INTEGER,
                        references: {
                          key: "InterestMasters",
                          value: "id"
                        },
                        allowNull: false
                      },
                      createdAt: {
                        allowNull: false,
                        type: Sequelize.DATE
                      },
                      updatedAt: {
                        allowNull: false,
                        type: Sequelize.DATE
                      }
                    });
                  });
              });
          });
      });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable("InterestMasters").then(function() {
      return queryInterface.dropTable("Interests").then(function() {
        return queryInterface.dropTable("companyDetails").then(function() {
          return queryInterface
            .dropTable("DesignationMasters")
            .then(function() {
              return queryInterface.dropTable("Users");
            });
        });
      });
    });
  }
};

1 个答案:

答案 0 :(得分:1)

validate参数中使用options函数:

{
  validate: {
    endYearIsAtLeastStartYear() {
      if (this.endYear < this.startYear) {
        throw new Error('End year must be equal to or higher than start year')
      }
    }
}