自v4.0.0起,必须明确提供方言/建议的修复程序尚未起作用

时间:2019-05-06 17:55:47

标签: mysql heroku sequelize.js

我一直在研究这个问题,但无法从任何帖子中找出解决方案。我希望有人可以帮我弄清楚这个问题,以便将我的应用程序部署到Heroku。

我尝试设置环境变量并在两个模型index.jsconfig.json中都包括方言。我在配置中声明了它。

config.json

{
  "development": {
    "username": "root",
    "password": null,
    "database": "ragnar_db",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "test": {
    "username": "root",
    "password": null,
    "database": "ragnar_db",
    "host": "127.0.0.1",
    "dialect": "mysql"
  },
  "production": {
    "use_env_variable": "JAWSDB_URL",
    "dialect": "mysql"
  }
}

modles/index.js

"use strict";

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

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

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

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

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

module.exports = db;

1 个答案:

答案 0 :(得分:0)

答案是通过这种方式为较旧的Sequelize版本提供代码。

if (config.use_env_variable) {
  var sequelize = new Sequelize(process.env[config.use_env_variable]);
  console.log(process.env);
} else {
  var sequelize = new Sequelize(
    config.database,
    config.username,
    config.password,
    {
      host: "host_name",
      dialect: "Your_dialect"
  });
}

另一个答案是重新初始化Sequelize-cli并运行用于配置数据库的命令。然后运行Sequelize -init,如果您通过终端进行了所有设置,则init将为环境变量存储JAWSDB_URL并将其设置在节点和heroku环境中。否则,它将要求您将JAWSDB_URL粘贴到终端查询器中。这是对旧版本的更改,以允许Sequelize比以前更好地隐藏数据库信息,但是如果您使用的是具有旧代码的最新版本,则必须同时在conf.json / config.js和models / index.js中。