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
Python 如何做插入。。。冲突时是否自动更新和更新时间戳?_Python_Postgresql_Psycopg2 - Fatal编程技术网

Python 如何做插入。。。冲突时是否自动更新和更新时间戳?

Python 如何做插入。。。冲突时是否自动更新和更新时间戳?,python,postgresql,psycopg2,Python,Postgresql,Psycopg2,我有一个使用PostgreSQL的表,与此类似: 我的表格: id col2 col3 import_ts TIMESTAMP(0) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP 因此,当我进行插入时,我不必为此列指定任何值,并且会自动插入当前时间戳 但是,当使用类似以下查询的查询时: INSERT INTO mytable (id, col2, col3) VALUES (%s, %s, %s) ON

我有一个使用PostgreSQL的表,与此类似:

我的表格:

id
col2
col3
import_ts TIMESTAMP(0) WITH TIME ZONE DEFAULT CURRENT_TIMESTAMP
因此,当我进行插入时,我不必为此列指定任何值,并且会自动插入当前时间戳

但是,当使用类似以下查询的查询时:

INSERT INTO mytable (id, col2, col3) VALUES (%s, %s, %s)
                            ON CONFLICT (id) DO UPDATE
                            SET col2 = %s, col3 = %s;
结果是,在
INSERT
的情况下,导入将按预期自动插入;但是在
更新
的情况下,
导入
列将不会更新

为了更新导入,当我使用
python
psycopg2
时,我可以添加
设置
导入=%s
并传递当前时间。然而,在一种情况下,时间戳是自动添加的,而在另一种情况下,我必须添加它,这似乎并不优雅和一致。我希望它总是由PostgreSQL自动完成。这可能吗?

当然可以,只需使用:


我想告诉SQL,我希望使用默认值,但这肯定很简单。谢谢你睁开我的眼睛;)
INSERT INTO mytable (id, col2, col3) VALUES (%s, %s, %s)
    ON CONFLICT (id) DO UPDATE
    SET col2 = %s, col3 = %s, import_ts = now();