node.js - 我正在使用 Node.js ORM Sequelize 及其命令行客户端,但我无法成功执行迁移

标签 node.js postgresql sequelize.js

在我的应用程序中,我使用 Sequelize-CLI 设置表并运行迁移到 PostgreSQL 数据库。这是我的代码:

.sequelizerc

var path = require("path");

module.exports = {
  'config': path.resolve('config', 'database.json'),
  'migrations-path': path.resolve('db', 'migrate')
}

配置/数据库.json
{
"development": {
    "username": "Sol",
    "password": null,
    "database": "experiments",
    "host": "127.0.0.1",
    "dialect": "postgres"
    },
"production": {
    "username": "Sol",
    "password": null,
    "database": "experiments",
    "host": "127.0.0.1",
    "dialect": "postgres"
    } 
}

我在我的模型目录中创建了两个模型,并使用 sequelize migration:create 创建了一个迁移文件在我的 db/migrate 目录中。最后我运行 sequelize db:migrate运行迁移,但是当我检查 PostgreSQL 数据库时,没有创建新表。运行命令后我得到这个响应:
Loaded configuration file "config/database.json".
Using environment "development".
    at new module.exports.Sequelize (/Users/Sol/src/projects/tests/Node.js/toVisit/node_modules/sequelize/lib/sequelize.js:182:13)
    at getSequelizeInstance (/usr/local/lib/node_modules/sequelize-cli/lib/tasks/db.js:203:10)
    at getMigrator (/usr/local/lib/node_modules/sequelize-cli/lib/tasks/db.js:208:21)
    at Object.module.exports.db:migrate.task (/usr/local/lib/node_modules/sequelize-cli/lib/tasks/db.js:23:7)
    at Gulp.gulp.task.aliases (/usr/local/lib/node_modules/sequelize-cli/lib/helpers/gulp-helper.js:14:14)
    at module.exports (/usr/local/lib/node_modules/sequelize-cli/node_modules/gulp/node_modules/orchestrator/lib/runTask.js:34:7)
    at Gulp.Orchestrator._runTask (/usr/local/lib/node_modules/sequelize-cli/node_modules/gulp/node_modules/orchestrator/index.js:273:3)
    at Gulp.Orchestrator._runStep (/usr/local/lib/node_modules/sequelize-cli/node_modules/gulp/node_modules/orchestrator/index.js:214:10)
    at Gulp.Orchestrator.start (/usr/local/lib/node_modules/sequelize-cli/node_modules/gulp/node_modules/orchestrator/index.js:134:8)
    at /usr/local/lib/node_modules/sequelize-cli/node_modules/gulp/bin/gulp.js:129:20
    at process._tickCallback (node.js:419:13)
    at Function.Module.runMain (module.js:499:11)
    at startup (node.js:119:16)
    at node.js:906:3

最佳答案

事实证明,问题是 Sequelize-CLI 无法找到 PostgreSQL,因为它没有必要的模块 pgpg-hstore安装。我在通过我的服务器以编程方式使用 Sequelize 时发现了这一点。它给了我一个错误,其中一部分可以在这里看到:

throw new Error('The dialect ' + this.getDialect() + ' is not supported.
        ^
Error: The dialect postgres is not supported. (Error: Cannot find module 'pg-hstore')

从那里,一个简单的谷歌搜索让我得到了更详细的解释,现在我可以成功地运行我的迁移了!

关于node.js - 我正在使用 Node.js ORM Sequelize 及其命令行客户端,但我无法成功执行迁移,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30553555/

相关文章:

json - 从 json 数据中解析出值

node.js - 蓝名单应用程序抛出 "Enroll failed to create remote cloudant database for Optional("test1")。超时

sql - 检索最佳结果的高效 SQL

javascript - 有条件的异步回调

sql - 从 postgresql 中的 jsonb 数组中获取元素作为平面字符串

C# NpgsqlCommand - AddWithValue 不是替代

mysql - 如何区分目标表和源表?

node.js - 如何创建一个固定长度的自动递增 id (sequelize)

javascript - 使用 Sequelize 如何指定要排序/限制的字段?

node.js - 如何在node.js中使用大量内存?