序列化外键和迁移的问题

时间:2019-02-21 00:55:59

标签: javascript sql sequelize.js

遇到使我的表具有关联性的问题。我似乎无法使用外键。

我认为这与多元化有关?或者我正在尝试同时使用belongsTo和hasMany for Company连接表。该表还将为员工打开另一个表。

我收到以下错误-错误:无法添加外键约束

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Industry = sequelize.define('Industry', {
    industry: DataTypes.STRING
  }, {
    underscored: true,
  });
  Industry.associate = function(models) {
    Industry.hasMany(models.Company, {
      foreignKey: 'industryId'
    })
  };
  return Industry;
};


'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Industries', {
      industryId: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      industry: {
        type: Sequelize.STRING
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn("NOW")
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn('NOW')
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Industries');
  }
};



'use strict';
module.exports = (sequelize, DataTypes) => {
  const Company = sequelize.define('Company', {
    businessName: DataTypes.STRING,
    img: DataTypes.STRING,
    address: DataTypes.STRING,
    summary: DataTypes.TEXT,
    point: DataTypes.GEOMETRY('POINT'),
  }, {
    underscored: true,
  });
  Company.associate = function(models) {
    Company.belongsTo(models.Industry, {
      foreignKey: 'industryId'
    });
    Company.hasMany(models.Employees, {
      foreignKey: 'companyId'
    });
  };
  return Company;
};

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Companies', {
      companyId: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      businessName: {
        type: Sequelize.STRING
      },
      img: {
        type: Sequelize.STRING
      },
      address: {
        type: Sequelize.STRING
      },
      summary: {
        type: Sequelize.TEXT
      },
      point: {
        type: Sequelize.GEOMETRY('POINT'),
        allowNull: false
      },
      created_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn("NOW")
      },
      updated_at: {
        allowNull: false,
        type: Sequelize.DATE,
        defaultValue: Sequelize.fn('NOW')
      },
      industryId: {
        type: Sequelize.INTEGER,
        allowNull: false,
        foreignKey: true,
        references: {
          model: 'Industries',
          key: 'industryId'
        }
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Companies');
  }
};

0 个答案:

没有答案
相关问题