【问题标题】:Failed to resolve config file, knex cannot determine where to generate migrations无法解析配置文件,knex 无法确定在哪里生成迁移
【发布时间】:2021-03-17 00:17:42
【问题描述】:

我正在尝试在某个目录(数据库目录)中迁移和播种。但是当我运行时:

npx knex migrate:make testing_table

它显示:

TypeError [ERR_INVALID_ARG_TYPE]: The "path" argument must be of type string. Received undefined
    at validateString (internal/validators.js:124:11)

目录

  • 主文件夹
  • 客户
  • 服务器
    • 数据库
      • 迁移
      • Knexfile.js
    • 公开
  • Package.json

我所做的是将 knexfile.js 移到数据库目录之外并进入主目录。它可以工作并进行迁移,但它会在主文件夹中创建另一个迁移文件夹,而不是在当前迁移文件夹中创建表。

代码如下:

连接:

const config = require('../db/knexfile')
const env = process.env.NODE_ENV || 'development'
const connection = knex(config[env])

module.exports = connection

knex 文件:


module.exports = {
  development: {
    client: 'sqlite3',
    connection: {
      filename: './dev.sqlite3'
    },
    useNullAsDefault: true,
    // Getting SQlite to honour contraints
    pool: {
      afterCreate: (conn, cb) =>
        conn.run('PRAGMA foreign_keys = ON', cb)
    }
  },

  test: {
    client: 'sqlite3',
    connection: {
      filename: ':memory:'
    },
    useNullAsDefault: true,
    seeds: {
      directory: path.join(__dirname, 'tests/seeds')
    },
    migrations: {
      directory: path.join(__dirname, 'migrations')
    }
  },

  production: {
    client: 'sqlite3',
    connection: process.env.DATABASE_URL,
    pool: {
      min: 2,
      max: 10
    },
    migrations: {
      directory: path.join(__dirname, './server/db/migrations')
    },
    seeds: {
      directory: path.join(__dirname, './seeds')
    }
  }
}

【问题讨论】:

  • 问题是什么? ;-)
  • 为没有澄清而道歉。如何从特定目录迁移表?
  • 我编辑了这篇文章,希望它更有意义。
  • 看看npx knex --help,看看您是否可以从源代码之外指定配置。

标签: database path migration knex.js


【解决方案1】:

如果您已将 knexfile 移动到主文件夹中(正如您在文件夹结构中提到的),您必须更改迁移目录的路径,如下所示:-

test: {
client: 'sqlite3',
connection: {
  filename: ':memory:'
},
useNullAsDefault: true,
seeds: {
  directory: path.join(__dirname, 'tests/seeds')
},
migrations: {
  directory: path.join(__dirname, '../Server/Database/Migrations') //--Changed this line
}},   

【讨论】:

    猜你喜欢
    • 2019-12-27
    • 2018-03-01
    • 1970-01-01
    • 2019-03-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-10-15
    • 1970-01-01
    相关资源
    最近更新 更多