Knex:为什么调用 "create table" 没有下一次调用 "then"

Knex: why does not the call "create table" perform without next call of "then"

我遇到了下一个问题。如果我尝试创建 table,调用这些方法 - 没有结果:

    this.knexDB.schema
        .createTable("students", table => {
            table.increments("id");
            table.string("student_name");
            table.string("studnt_number");
        });

table不创建。 但是,如果我添加 "then" 的调用 - table 会立即创建:

    this.knexDB.schema
        .createTable("students", table => {
            table.increments("id");
            table.string("student_name");
            table.string("studnt_number");
        })
        .then(() => {
            console.log("table created!");
        });

问题:

  1. 为什么会这样?
  2. 有什么方法可以使用更多 "appropriate" 方式立即调用 create table 吗?

Knex 是一个查询构建器库,它具有可链接的 api,允许您链接不同的方法调用来构建查询。

由于 db 查询实际上是一个异步操作,lib 作者决定 "invoke" db 查询调用伪承诺,如 api。因此需要调用then.

如果您正在使用 asyncawait,您可以使用它,wait 将隐式调用 then

await this.knexDB.schema.createTable('students', table => {
  table.increments('id');
  table.string('student_name');
  table.string('studnt_number');
});