使用Sequalize从模型迁移

时间:2017-11-14 22:23:19

标签: javascript node.js migration models

我已经完成了模型,我需要迁移它们。我尝试了命令sequalize migrate:generate。 像这个sequalize migrate:generate - 模型路径(/模型没有工作),但我不明白它是如何工作的。有没有办法自动生成它们或者不可能?如果是这样,我该怎么做?

我实际上使用节点作为我的后端

项目结构如下:

--api
----models
-------mymodels.js
-------index.js
----migrations

我的index.js:

"use strict";

const fs = require("fs");
const path = require("path");
const Sequelize = require("sequelize");
const basename = path.basename(module.filename);
const env = process.env.NODE_ENV || "development";
const config = require(__dirname + "/../config/config.json")[env];
const db = {};

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;
  });

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

// RELATIONSHIP FIELDS

有人可以帮助我解决这个我无法解决的问题

0 个答案:

没有答案