Results 1 to 1 of 1
  1. #1
    Join Date
    Jun 2016

    Unanswered: Update JSON field entirely from python

    Hi folks,

    I'm newbie with postgres. I spent a day trying to make it clear, but unfortunately with no success.

    I have a table:

    CREATE TABLE backlinks (
        target varchar(2048) PRIMARY KEY,
        refs JSON NOT NULL
    I use psycopg2 library to interact with the postres. I don't use any ORM due to I only need a couple of inserts and updates.

    Below the python code I use to insert
    connection = psycopg2.connect(...)
    cursor = connection.cursor
    cursor.execute("insert into backlinks (target, refs) values (%s, to_json(%s))",
                         (target_deep_link, [Json(ref_hosts)]))
    Where ref_hosts is python dict.

    The insert works as expected, but I can't figure out how to update the JSON field entirely.
    I try the code as follows, but it seems it doesn't work.

    cursor.execute("update backlinks set refs = to_json(%s) where target = %s",
                        ([Json(ref_hosts)], str(target_deep_link)))
    Where am I wrong?

    I'm on Ubuntu 14.04 with Postrges 9.3.13.

    Thanx in advance.

    UPDATED: I really confused, some records are updated, but other left original. Also I have no error while cursor.execute().
    Last edited by 4xy; 06-29-16 at 14:18.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts