简体   繁体   中英

Unable to update a JsonField in Postgres using raw queries in Django

I'm trying to run this code after a migration.

cursor.execute(f'UPDATE abc_model SET "abc_temp" = \'{{"0" : "{abc}" }}\' WHERE abc_model."id" = {id}')

I'm getting this error when using a dump of the following version:

-- Dumped from database version 9.5.25
-- Dumped by pg_dump version 9.5.25
django.db.utils.DataError: invalid input syntax for type json
LINE 1: ...abc_model SET "abc_temp" = '{"0" : "M...
                                      ^
DETAIL:  Character with value 0x0a must be escaped.
CONTEXT:  JSON data, line 1: ...cum current detected the deviation from baseline.

It works fine when using a dump of the following version:

-- Dumped from database version 12.10 (Ubuntu 12.10-1.pgdg18.04+1)
-- Dumped by pg_dump version 14.2 (Ubuntu 14.2-1.pgdg18.04+1)

I'm not sure if this issue is being caused just by the difference in the dump db version or because of encoding or something else.

Try

cursor.execute(f”””UPDATE abc_model SET abc_temp = '{{"0" : "{abc}" }}' WHERE abc_model.id = {id}”””)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM