使用cocece的问题,而在更新中没有任何问题
我正在使用psycopg2从python更新postgres表,如下
表:tab1
+------------------+-------------------+-------------------+-------------------+
|Id |name |state |added_time |
+------------------+-------------------+-------------------+-------------------+
|123 |test1 |True |2022-04-29 09:08:46|
+------------------+-------------------+-------------------+-------------------+
架构:
Id: Int8
Name: text
state: boolean
added_time: timestamp
查询我正在创建以更新数据如下所示,
sql=UPDATE tab1 as org SET "name"=coalesce(new."name", org."name"),"state"=coalesce(new."state", org."state"),"added_time"=coalesce(new."added_time", org."added_time")
FROM (VALUES %s) as new ("name", "state", "added_time") WHERE (new."Id"=org."Id")
最后以值as as as a as a as as fulection as a greation as at as a
conn = psycopg2.connect(db_connection)
cur = conn.cursor()
psycopg2.extras.execute_values(cur, sql, val)
val = [123, "test", None, None]
as a s py> psycopg2.errors.datataTypemant:coccocce:coccaTCece:类型文本和布尔值无法匹配
状态列的,并且当值无。如果已经存在非零值,我正在使用cocce避免更新零值。 我尝试使用nullif,但它不起作用,还尝试打印mogrified查询以查看传递的实际值,
UPDATE tab1 as org SET "name"=coalesce(new."name", org."name"),"state"=coalesce(new."state", org."state"),"added_time"=coalesce(new."added_time", org."added_time") \n FROM (VALUES ARRAY[123,"test",NULL,NULL]) as new ("Id","name","state","added_time") WHERE (new."Id"=org."Id")
有人可以在这里提供帮助吗?
I am updating postgres table from python using psycopg2 like below
Table: tab1
+------------------+-------------------+-------------------+-------------------+
|Id |name |state |added_time |
+------------------+-------------------+-------------------+-------------------+
|123 |test1 |True |2022-04-29 09:08:46|
+------------------+-------------------+-------------------+-------------------+
Schema:
Id: Int8
Name: text
state: boolean
added_time: timestamp
Query I am creating to update data is as below,
sql=UPDATE tab1 as org SET "name"=coalesce(new."name", org."name"),"state"=coalesce(new."state", org."state"),"added_time"=coalesce(new."added_time", org."added_time")
FROM (VALUES %s) as new ("name", "state", "added_time") WHERE (new."Id"=org."Id")
and finally running query with values as,
conn = psycopg2.connect(db_connection)
cur = conn.cursor()
psycopg2.extras.execute_values(cur, sql, val)
val = [123, "test", None, None]
I am getting exception as psycopg2.errors.DatatypeMismatch: COALESCE types text and boolean cannot be matched
for state column and same is occurring for added_time when values are None. I am using coalesce to avoid updating null values if non null value is already present.
I have tried using nullif but it didn't work and also tried printing mogrified query to see what actual values are passed,
UPDATE tab1 as org SET "name"=coalesce(new."name", org."name"),"state"=coalesce(new."state", org."state"),"added_time"=coalesce(new."added_time", org."added_time") \n FROM (VALUES ARRAY[123,"test",NULL,NULL]) as new ("Id","name","state","added_time") WHERE (new."Id"=org."Id")
Can anyone help here?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论