Javascript 使用knex动态创建联接表

Javascript 使用knex动态创建联接表,javascript,postgresql,seed,knex.js,Javascript,Postgresql,Seed,Knex.js,我现在正在学习knex,并尝试将两个不同表中的id号动态插入空表中,该空表稍后将用作联接表 到目前为止,这就是我所拥有的,但我觉得我现在离本垒太远了 exports.seed = function(knex, Promise) { return knex('future_join_table').del() .then(function () { return Promise.all([ // Inserts seed entries

我现在正在学习knex,并尝试将两个不同表中的id号动态插入空表中,该空表稍后将用作联接表

到目前为止,这就是我所拥有的,但我觉得我现在离本垒太远了

exports.seed = function(knex, Promise) {
    return knex('future_join_table').del()
      .then(function () {
        return Promise.all([
        // Inserts seed entries
        knex('future_join_table').insert({
          first_id: knex('table1').select('id'),
          second_id: knex('table2').select('id')
        })
      ]);
    });
};
上面的代码假设您的
表1
表2
具有相同的项数

我不知道为什么要用这种方式创建中间表,但到目前为止,我们无法根据您的信息执行任何连接操作

上面的代码假设您的
表1
表2
具有相同的项数

我不知道为什么要用这种方式创建中间表,但到目前为止,我们无法根据您的信息执行任何连接操作

exports.seed = (knex) => {
  let table1Array = [];
  const toBeInsertRows = []
  return knex('future_join_table').del()
  .then(() => knex.select('id').from('table1'))
  .then((rows) => {
    table1Array = rows;
    return knex.select('id').from('table2')
  })
  .then((rows) => {
    rows.forEach((row, index) => toBeInsertRows.push({ first_id: table1Array[index].id, second_id: row.id }))
    return knex('future_join_table').insert(toBeInsertRows);
  });
}