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

Postgresql and lastInsertId()

    XMLWordPrintable

Details

    • Suggestion
    • Resolution: Done
    • Not Evaluated
    • 5.1.0
    • 4.6.3
    • SQL Support
    • None
    • f4d7b4d10f7d3badbb0ac5ad5c0754a044f8bdd2

    Description

      Driver for Postgresql returns OIDS from last insert. In documentation of Postgresql 8.0 and 9.0 there is a tip: "Using OIDs in new applications is not recommended(...)".

      Now I must write something like this:
      QSqlQuery query( "INSERT INTO some_table( field1, field2 ) VALUES( 'val1', 'val2' )" );
      qDebug() << query.lastInsertId(); // here we have a OID for example 123456

      Then if we want to get id of new record we must do:
      QSqlQuery query( "SELECT id FROM some_table WHERE OID = 123456" );

      After this select we have a id from last insert. This is difficult and not friendly. Maybe insert in postgresql should use a RETURNING in INSERT statement.

      QSqlTableModel can't return a OIDs. If we make:

      QSqlTableModel tabModel;
      tabModel.setTable( "some_table" );

      QSqlRecord rec = tabModel.record();
      rec.setValue( "field1", "value1" );
      rec.setValue( "field2", "value2" );

      tabModel.insertRecord( -1, rec ); // append a record

      tabModel.query().lastInsertId(); // return 0

      I think that after insertRecord a model make select() and then query return 0. In my opinion lastInsertId in situation with QSqlTableModel is broken.

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            mabrand Mark Brand
            hostel Lukasz
            Votes:
            1 Vote for this issue
            Watchers:
            3 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes