Uploaded image for project: 'Qt'
  1. Qt
  2. QTBUG-82425

PostgreSQL driver: setAutoValue() missing

    XMLWordPrintable

Details

    • Bug
    • Resolution: Incomplete
    • P3: Somewhat important
    • None
    • 5.14.1
    • SQL Support
    • None
    • All

    Description

      Hi there,

       

      When doing a QSqlTableModel->insertRecord( row, record_with_pk_field_set_to_setGenerated(false) ) against a SQLite table containing a Primary Key, then a new row is inserted into the model, and it is visible, just as one would expect, WITHOUT calling QSqlTableModel->select() after the insertion.

       

      Doing the very same thing against a PostgreSQL table REQUIRES calling select() after the insertion. Otherwise, an empty row is inserted into the model. Note, calling selectRow(row) to update the inserted row does not work (for the same reason: the model is missing the PK).

       

      Perusing the SQLite and the PostgreSQL drivers, I found that the PG driver is lacking the functionality to detect the PK field and flag it as setAutoValue(true). Therefore, I ran the SQL query the driver is using to return the table record() field prototypes, in order to figure out if there is way to find the PK field and flag it with setAutoValue(true).

       

      This is the result when I run the SQL query taken from the driver again my table:

       

      monospaced

      attname | atttypid | attnotnull | attlen | atttypmod | pg_ge
      t_expr
      --------------------------------------------+-------------------
      -----------------------
      item_name | 20921 | t | -1 | -1 |
      comment | 25 | f | -1 | -1 |
      frequency | 23 | f | 4 | -1 |
      tag_id | 23 | f | 4 | -1 |
      unix_time | 23 | f | 4 | -1 |
      id | 23 | t | 4 | -1 | nextval('shared.desk
      top_id_seq'::regclass)
      monospaced

       

      Since I am not an expert in all things PostgreSQL, I simply "patched" the driver by checking if the "default value" field (in the driver's opinion) contains the string "nextval". If it does, then I consider the field to be the PK and flag it as setAutoValue(). See attached file.

       

      While this is working fine for me (no more select() after insert_record() required), I would not bet my life on it working for other people/circumstances, too.

       

      So, I would like to suggest someone who knows their PostgreSQL drivers to have a look at this issue, and implement a proper way of detecting a PK field and flagging it setAutoField(true).

       

      Many thanks,

      Holger

      Attachments

        1. qtbug82425.zip
          3 kB
        2. setAutoValue.patch
          0.9 kB
        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            andysh Andy Shaw
            Fantail Holger Jahn
            Votes:
            0 Vote for this issue
            Watchers:
            4 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes