【问题标题】:I am having this Issue when trying to deploy my API to heroku尝试将我的 API 部署到 heroku 时遇到此问题
【发布时间】:2020-03-28 04:53:21
【问题描述】:

我已经从 Heroku 安装了 PostgreSQL 插件,但是我在后端使用了 Knex.js,就像我之前使用 SQLite 一样,我遇到了以下错误。

我正在为网站和移动应用程序构建后端并尝试将其部署到 Heroku,在开发时我使用 SQLite,但我发现由于我使用的是 Knex.js,因此我可以轻松地转移到 Postgres 附加组件来自 Heroku。在postbuild 上运行knex migrate:latest 时,我遇到了这个问题。

no such file or directory, scandir '/tmp/build_46fb7aa66e7e3cea06d2f04a21ad9249/migrations'

这是我的 knex 文件:

// Update with your config settings.

module.exports = {

  development: {
    client: 'sqlite3',
    connection: {
      filename: './src/database/db.sqlite'
    },
    migrations: {
      directory: './src/database/migrations'
    },
    useNullAsDefault: true
  },


  test: {
    client: 'sqlite3',
    connection: {
      filename: './src/database/test.sqlite'
    },
    migrations: {
      directory: './src/database/migrations'
    },
    useNullAsDefault: true
  },

  staging: {
    client: 'postgresql',
    connection: {
      database: 'my_db',
      user:     'username',
      password: 'password'
    },
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    }
  },

  production: {
    client: 'pg',
    debug: true,
    connection: process.env.DATABASE_URL,
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations'
    },
    ssl: true
  }

};

和我的关系

const knex = require('knex')
const configuration = require('../../knexfile')

const config = process.env.NODE_ENV


const connection = knex(configuration[config])


module.exports = connection

测试和开发的迁移工作正常

我也不知道这之后是否会起作用,所以如果有人有任何经验,我可以使用任何帮助

【问题讨论】:

  • "但我认为由于我使用的是 knex,因此我可以轻松地从 heroku 转移到 Postgre 插件"——这是个坏主意。 始终使用您在生产中定位的同一数据库进行开发。即使有像 knex 这样的库,数据库引擎也不能互相替代。
  • 为什么不在生产配置中提供迁移目录?
  • 哦,可能是,我去查一下
  • 成功了!谢谢

标签: node.js postgresql heroku knex.js


【解决方案1】:

Heroku 似乎找不到您的迁移。

您的生产配置似乎不包含迁移目录。尝试添加它,例如

  production: {
    client: 'pg',
    debug: true,
    connection: process.env.DATABASE_URL,
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      tableName: 'knex_migrations',
      directory: './src/database/migrations'  // <-- here
    },
    ssl: true
  }

在开发时我使用的是 SQLite,但发现由于我使用的是 Knex.js,我可以轻松地从 Heroku 转移到 Postgres 插件

我敦促您在开发和生产中使用相同的数据库。即使您使用的是 Knex 之类的东西,数据库引擎也不会相互替代。

【讨论】:

    猜你喜欢
    • 2018-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-08-27
    • 2012-05-19
    • 2022-01-25
    • 2017-05-03
    相关资源
    最近更新 更多