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

QSqlRelationalTableModel - relation refresh crash

XMLWordPrintable

    • c0aabbd8a (dev), 4ab6fc721 (6.8), 6feeb7250 (6.7), a3ef7eed3 (tqtc/lts-6.5)

      If multiple relations are set on a QSqlRelationalTableModel, the relations can crash when updating the relation model values.

      QSqlRelationalTableModel model;
      model.setTable("table");
      model.setRelation(0, QSqlRelation("table2", "id", "Name"));
      model.setRelation(1, QSqlRelation("table3", "id", "Name"));
      
      // Use model
      
      // Foreign keys changed, update models
      QSqlTableModel* relationModel = model.relationModel(0);
      if(relationModel)
      {
        relationModel->select(); // Crash here
      }
      

      Looking at the internal code:
      https://github.com/qt/qtbase/blob/dev/src/sql/models/qsqlrelationaltablemodel.cpp
      QSqlRelationalTableModel::setRelation()

      Resizes the d->relations vector.
      However, each QRelatedTableModel stores an internal pointer into this array. This pointer becomes invalid when a new relationship is added and the array is resized.

      A potential fix is to either update all existing pointers on array resize, or store a parent class pointer and index in QRelatedTableModel instead of a pointer to the array element.

      A workaround is to set a dummy relation on a column that exceeds the data column size on creation.
      QSqlRelationalTableModel::setRelation(ModelColumnCount + 1, QSqlRelation())
      This pre-sizes the internal array, so a later resize does not need to occur.

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

            chehrlic Christian Ehrlicher
            d_trebilco Damain T
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: