Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何使用knex和postgresql有条件地更新多行?_Postgresql_Knex.js_Bookshelf.js - Fatal编程技术网

如何使用knex和postgresql有条件地更新多行?

如何使用knex和postgresql有条件地更新多行?,postgresql,knex.js,bookshelf.js,Postgresql,Knex.js,Bookshelf.js,下面是一个表格布局的示例 CREATE TABLE test (id1 int, id2 int, auth boolean); INSERT INTO test VALUES (1, 1, true); 我正在尝试将以下查询转换为knex.js框架 UPDATE test as t SET auth = c.auth from (values (1, 1, false),(2, 1, false)) as c(id1, id2, auth) where c.

下面是一个表格布局的示例

CREATE TABLE test (id1 int, id2 int, auth boolean);
INSERT INTO test VALUES (1, 1, true);
我正在尝试将以下查询转换为knex.js框架

UPDATE test as t 
    SET auth = c.auth
    from (values (1, 1, false),(2, 1, false))
    as c(id1, id2, auth)
    where c.id1 = t.id1 AND c.id2 = t.id2;

select * from test
这是一把小提琴:

我环顾四周,发现了以下资源:

在尝试实现这些方法之后,我仍然没有成功,也不确定我会错在哪里

我试图通过将原始postgres查询包装到knex.raw语句中来强制执行查询,如:

return knex.raw('' +
    'UPDATE test as t ' +
    'SET auth = c.auth ' +
    'from (values (1, 1, false),(2, 1, false))' +
    'as c(id1, id2, auth)' +
    'where c.id1 = t.id1 AND c.id2 = t.id2;')
在«as»

我还尝试使用以下方法遵循github问题建议:

 let string = knex
        .into('test t')
        .update('auth, c.auth')
        .values('(1, 1, false), (2, 1, false)')
        .where(knex.raw('c.id1 = t.id1 AND c.id2 = t.id2'))
        .toString()

        knex.raw(string)
返回错误“values”的不是函数


我对knex和postgres都是新手,所以我不确定我是否错过了一些显而易见的痛苦经历!非常感谢您的帮助。

在原始版本中,您可能需要在
false)as
处的“as”前面加一个空格,才能变成
false)as

我通过添加('query-error'),看到了这一点,如下所示。通过这些信息,您应该能够确定这是SQL引擎错误还是Knex错误,以及SQL是否按照您的需要生成

return knex.raw('' +
    'UPDATE test as t ' +
    'SET auth = c.auth ' +
    'from (values (1, 1, false),(2, 1, false)) ' +
    'as c(id1, id2, auth)' +
    'where c.id1 = t.id1 AND c.id2 = t.id2;')
    .on('query-error', function(ex, obj) {
        console.log("KNEX query-error ex:", ex, "obj:", obj);
    }).then(function(retVal) {
        console.log("Query ran okay.");
        return retVal;
    });
还有('query')上的
.on,
,它将为您提供有关查询的信息,以帮助您正确执行查询。请参阅:

问候,, 加里