0

I am using sequelize cli and got each migration file for each model generated, I am trying to generate all the tables with a single migration file as they are dependent on each other, but I am keep getting this error

cannot find property startYear of undefined

Also, I need a tutorial on how to change models in sequelize using 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");
            });
        });
      });
    });
  }
};
Drupad Singh
  • 48
  • 1
  • 4

1 Answers1

1

Use a validate function in the options argument:

{
  validate: {
    endYearIsAtLeastStartYear() {
      if (this.endYear < this.startYear) {
        throw new Error('End year must be equal to or higher than start year')
      }
    }
}
Maria Ines Parnisari
  • 16,584
  • 9
  • 85
  • 130
  • This is not the error I tried removing the startYear then it gives me the error "replace" not defined – Drupad Singh Jun 17 '18 at 17:21
  • If I am using multiple migrations files for multiple tables then how can I control table creation as tables are using foreign key so former tables must be created before them, how can I specify which migrations to take place first and executing dependent migrations later – Drupad Singh Jun 17 '18 at 19:13