在两个模式上迁移

Migration on two schemas

我的数据库上有 2 个模式:schema1public,我想在那里创建一些表。我在迁移中尝试了这样的代码:

return knex.schema

    .raw('CREATE SCHEMA IF NOT EXISTS schema1;')

    .createTableIfNotExists('table1', table => {
      table.increments('id')
    })

    .withSchema('public')

    .createTableIfNotExists('table2', table => {
      table.increments('id')
    })

我希望有:schema1.table1public.table2 但我没有。有什么想法吗?

Belayer 在评论中是非常正确的,但我将扩展为一个答案来演示语法。您还需要避免执行以下操作:

createTableIfNotExists('schema1.table1', table => {

per Identifier Syntax,这是行不通的:养成总是使用 withSchema.

的习惯

此外,我不建议在不等待前一个完成的情况下发布后续 createTable。虽然它 可能 有效,但我认为你会 运行 在关系等方面遇到麻烦。这使得你的原始代码更像:

exports.up = knex =>
  knex
    .raw("CREATE SCHEMA IF NOT EXISTS schema1")
    .then(() =>
      knex.withSchema("schema1").createTableIfNotExists("table1", table => {
        table.increments("id");
      })
    )
    .then(() =>
      knex.withSchema("public").createTableIfNotExists("table2", table => {
        table.increments("id");
      })
    );