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

QSqlTableModel: can not update an already modified row

    XMLWordPrintable

Details

    • Bug
    • Resolution: Unresolved
    • P2: Important
    • None
    • 6.8
    • SQL Support
    • None
    • All

    Description

      This bug report originally comes from SO:

      void printRecord(const QSqlRecord& record, const QString& msg)
      {
          qDebug() << "Name:" << record.value("Name").toString() << "isGenerated :" <<
                      (record.field("Name").isGenerated() ? "true " : "false") << "|" << msg; qDebug();
      }
      void printModel(const QSqlTableModel& model, const QString& msg)
      {
          printRecord(model.record(0), msg);
      }
      
      void main()
      {
          QCoreApplication app(argc, argv);
          QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
          db.setDatabaseName(":memory:");
          db.open();
      
          QSqlQuery query;
          query.exec("CREATE TABLE Person(Name TEXT);");
          query.exec("INSERT INTO Person (Name) VALUES ('John Green');");
      
          QSqlTableModel model(nullptr, db);
          model.setTable("Person");
          model.setEditStrategy(QSqlTableModel::OnManualSubmit);
          model.select(); printModel(model, "Model after select");
      
          qDebug() << "-------------------"; qDebug();
      
          auto record = model.record(0); printRecord(record, "Record retrieved first time");
          model.setRecord(0, record); printModel(model, "Model after setRecord first time");
      
          qDebug() << "-------------------"; qDebug();
      #if 0
          record = model.record(0); printRecord(record, "Record retrieved second time");
          model.setRecord(0, record); printModel(model, "Model after setRecord second time");
      
          qDebug() << "-------------------"; qDebug();
      #endif
          record.setValue("Name", "Andy Brown"); printRecord(record, "Record modified");
          model.setRecord(0, record); printModel(model, "Model after setRecord after modifying record");
      
          qDebug() << "-------------------"; qDebug();
      
          if (!model.submitAll())
              qDebug() << model.lastError();
          printModel(model, "Model after submitAll");
      
          query.exec("SELECT * FROM Person;");
          query.next();
          qDebug() << "Content of DB Table \"Name\":" << query.value("Name").toString();
          qDebug();
          return 0;
      }
      

      When a row is updated once and not submitted to the database, a second update will screw up the internal cache and a submit() will not update the row in the database.

      Attachments

        For Gerrit Dashboard: QTBUG-132708
        # Subject Branch Project Status CR V

        Activity

          People

            chehrlic Christian Ehrlicher
            chehrlic Christian Ehrlicher
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

            Dates

              Created:
              Updated:

              Gerrit Reviews

                There is 1 open Gerrit change