Knex 在插入后解析空集

Knex resolving empty set after insert

我正在尝试使用 Postgresql 和 Knex 在 Node 中设置一个基本的数据库连接,但我无法让看似简单的插入工作。我将此代码的大部分基于 Knex github 存储库中的示例代码。

问题似乎是我的第一个插入(我添加管理员用户的地方)正在解析一组空行(不确定,因为文档不包含关于行或行集的任何信息我能找到)。

这是我的代码:

const knex = require("knex")({
  client: "postgres",
  connection: {
    host     : "127.0.0.1",
    user     : "postgres",
    password : "my password goes here",
    database : "issue-tracker",
    charset  : "utf8"
  }
});

knex.schema
  .dropTableIfExists("tickets")
  .dropTableIfExists("users")
  .createTable("users", createUserTable)
  .createTable("tickets", createTicketTable)
  .then(() => addTestData())
  .then(() => console.log("Done"))
  .catch((e) => console.log(e));

function createUserTable(table) {
  table.increments("id");
  table.string("username").index().unique();
  table.string("password");
  table.timestamps();
}

function createTicketTable(table) {
  table.increments("id");
  table.timestamps();
  table.integer("creator_id").unsigned().references("users.id");
  table.string("text");
}

function addTestData() {
  return Promise.resolve()
    .then(() =>
      knex.insert({username: "admin", password: "password"})
      .into("users")
    )
    .then((rows) =>
      // rows is empty?
      knex.insert({creator_id: rows[0], text: "this is a test"})
      .into("tickets")
    )
    .then(() =>
      knex("users").join("tickets", "users.id", "tickets.creator_id")
      .select("users.id as creator_id", "users.username as creator_name", "tickets.text as text")
    )
    .then(console.log.bind(console));
}

如有任何帮助,我们将不胜感激。

Promise 处理程序必须 return 一些东西。它像流水线一样工作 - 每个站点都必须将其工作结果放回流水线上。

  • knex 中的插入操作不会 return 行,除非您明确告诉他们这样做。 Knex 有 .returning() (docs)。
  • 你在链中的最后一个处理程序(console.log.bind(console))return什么都没有,因此链的最终结果是未定义的。
  • 您不需要使用 Promise.resolve() 启动承诺链。您可以使用任何 promise-returning 函数启动它 - 在这里直接使用 knex() 是明智的。

所以,稍微重新安排一下,我们最终得到:

function addTestData() {
    return knex("users")
        .returning("user_id")
        .insert({username: "admin", password: "password"})
        .then((rows) => {
            return knex("tickets")
                .returning("ticket_id")
                .insert({creator_id: rows[0], text: "this is a test"});
        })
        .then((rows) => {
            return knex("users")
                .join("tickets", "users.id", "tickets.creator_id")
                .select("users.id as creator_id", "users.username as creator_name", "tickets.text as text");
        })
        .then((rows) => {
            console.log.bind(console);
            return rows;
        });
}

这等同于此,其中隐含了 return,因此不太明显:

function addTestData() {
    return knex("users")
        .returning("user_id")
        .insert({username: "admin", password: "password"})
        .then((rows) => knex("tickets")
            .returning("ticket_id")
            .insert({creator_id: rows[0], text: "this is a test"});
        })
        .then((rows) => knex("users")
            .join("tickets", "users.id", "tickets.creator_id")
            .select("users.id as creator_id", "users.username as creator_name", "tickets.text as text");
        })
        .then((rows) => {
            console.log.bind(console);
            return rows;
        });
}