顺序迁移不会从模型中迁移多个字段

时间:2019-08-03 13:13:04

标签: javascript node.js postgresql sequelize.js

我正在尝试将新表迁移到Sequelize上,以在PostgreSQL 9.4上构建数据库。迁移之后,该表不会显示多个字段(第一个字段),并且我也无法使用我的API来请求它。

我尝试使用ES5和ES6编写模型,撤消所有迁移并重新迁移,并尝试使用其他表手动编写模型

这是我的index.js用于续集:

[...]
let sequelize;
if (config.use_env_variable) {
  sequelize = new Sequelize(process.env[config.use_env_variable]);
} else {
  sequelize = new Sequelize(
    config.database, config.username, config.password, config
  );
}

fs
  .readdirSync(__dirname)
  .filter((file) =>
    (file.indexOf('.') !== 0) &&
    (file !== basename) &&
    (file.slice(-3) === '.js'))
  .forEach((file) => {
    const model = sequelize.import(path.join(__dirname, file));
    db[model.name] = model;
  });

Object.keys(db).forEach((modelName) => {
  if (db[modelName].associate) {
    db[modelName].associate(db);
  }
});

db.sequelize = sequelize;
db.Sequelize = Sequelize;

module.exports = db;

这是我的桌子的模型文件(名为电影)

'use strict';
module.exports = (sequelize, DataTypes) => {
  const Movies = sequelize.define('Movies', {
    title: DataTypes.STRING,
    year: DataTypes.INTEGER,
    runtime: DataTypes.STRING,
    genre: DataTypes.STRING,
    plot: DataTypes.STRING,
    documentary: DataTypes.BOOLEAN,
    votes: DataTypes.INTEGER
  }, {});
  Movies.associate = function(models) {
    // associations can be defined here
  };
  return Movies;
};

这是迁移文件:

'use strict';
module.exports = {
  up: (queryInterface, Sequelize) => {
    return queryInterface.createTable('Movies', {
      id: {
        allowNull: false,
        autoIncrement: true,
        primaryKey: true,
        type: Sequelize.INTEGER
      },
      title: {
        type: Sequelize.STRING
      },
      year: {
        type: Sequelize.NUMBER
      },
      runtime: {
        type: Sequelize.STRING
      },
      genre: {
        type: Sequelize.STRING
      },
      plot: {
        type: Sequelize.STRING
      },
      documentary: {
        type: Sequelize.BOOLEAN
      },
      votes: {
        type: Sequelize.NUMBER
      },
      createdAt: {
        allowNull: false,
        type: Sequelize.DATE
      },
      updatedAt: {
        allowNull: false,
        type: Sequelize.DATE
      }
    });
  },
  down: (queryInterface, Sequelize) => {
    return queryInterface.dropTable('Movies');
  }
};

我希望除了标题字段外还有更多的字段,但是它总是创建只有“ id,title,createdAt,updateAt”字段的电影。

0 个答案:

没有答案