当值的列之一仅包含Null
值时,需要帮助来解决问题。例子:
create table foo (
id serial constraint foo_pk primary key,
a int,
b int
);
insert into foo (a,b) values (1, 1), (2, 2);
update foo t
set a = v.a, b = v.b
from (values (1, NULL, 1),(2, NULL, 2)) as v(id, a, b)
where v.id = t.id;
这给了我:
[42804] ERROR: column "a" is of type integer but expression is of type text
Hint: You will need to rewrite or cast the expression. Position: 22
我正在使用psycopg2.extras.execute_values
以更新多个 Django ORM 对象。寻找不需要将空值显式转换为字段类型的解决方案。