如何一次插入两个表?准备好的发言

How to insert in two tables in one time? prepared statements

本文关键字:准备好 两个 插入 何一次      更新时间:2023-09-26

如何一次插入两个表
我需要插入第二个表user_information字段user_id,第一个表user插入返回id,我找到了这个答案,但我找不到如何使用params准备语句

var dbQuery = 'WITH insertUser AS (
  INSERT INTO "user" (status, create_date) VALUES ($1, $2) RETURNING id
  )
  , insertUserInformation AS (
  INSERT INTO user_information (user_id, email) VALUES ($3, $4)
  )
';
yield queryPromise(dbClient, dbQuery, [status, timestamp, ??, email]);

pg

使用事务。这样,要么提交所有查询,要么不提交任何查询。并且在执行完所有查询之前的不完整状态对于其他进程是不可见的。

有关如何在node-postgres中执行事务的更多信息,请访问https://github.com/brianc/node-postgres/wiki/Transactions

作为参考,最相关的部分是:

var Client = require('pg').Client;
var client = new Client(/*your connection info goes here*/);
client.connect();
var rollback = function(client) {
  //terminating a client connection will
  //automatically rollback any uncommitted transactions
  //so while it's not technically mandatory to call
  //ROLLBACK it is cleaner and more correct
  client.query('ROLLBACK', function() {
    client.end();
  });
};
client.query('BEGIN', function(err, result) {
  if(err) return rollback(client);
  client.query('INSERT INTO account(money) VALUES(100) WHERE id = $1', [1], function(err, result) {
    if(err) return rollback(client);
    client.query('INSERT INTO account(money) VALUES(-100) WHERE id = $1', [2], function(err, result) {
      if(err) return rollback(client);
      //disconnect after successful commit
      client.query('COMMIT', client.end.bind(client));
    });
  });
});

这在postgresql中是不可能的。我通过创建函数并简单地使用参数执行,解决了完全相同的问题。正如我在您的表结构中看到的,您没有太多属性,所以这将相对容易。

示例代码:

function.sql

CREATE OR REPLACE FUNCTION createSomething
(
    IN attr1 VARCHAR(20),
    IN attr2 VARCHAR(200)
)
RETURNS void AS $$
DECLARE userId INTEGER;
BEGIN
    INSERT INTO table1 (col1, col2) VALUES
    (
        attr1,
        attr2
    ) RETURNING id INTO userId;
    INSERT INTO table2 (user_id, col11, col2) VALUES
    (
        userId,
        col11,
        col12
    );
END;
$$ LANGUAGE plpgsql;

用法:

SELECT createSomething('value1', 'value2');

请注意,第二个insert语句将知道最近用户的id是什么,并将使用它。

PostgreSQL Prepared Statements不会让你这么做。你必须使用事务。

下面是您使用pg promise实现的示例,使用ES7语法:

const pgp = require('pg-promise')({
    // initialization options;
});
const db = pgp(/* your connection object or string */);
db.tx(async t => {
        const user = await t.one('INSERT INTO user(status, create_date) VALUES($1, $2) RETURNING id', [status, timestamp]);
        return t.none('INSERT INTO user_information(user_id, email) VALUES($1, $2)', [user.id, email]);
    })
    .then(() => {
        // SUCCESS;
    })
    .catch(error => {
        // ERROR;
    });

我不认为这可以作为一个自然的sql语句来实现。你必须把它包装成一个过程或其他机制。