Qt World Summit: Register Today!

How to get a record to insertRecord(-1, rec) in QSqlRelationalTableModel

  • What is the best way to get a record to insert a new row to the base table with foreign keys?

    When using record() or QSqlTableModel::record() after setRelation(...), the returned record contains fields with the column names of the display column.
    A inserRecord( -1, row ) with these fields fails, because the fields are not present in the table?

    I would expect that QSqlTableModel::record() returns a useful record, or that a record with display columns is transformed back to foreign key columns?

    My solution is to store a copy of original record before setting the relations.
    Is there a more generic solution?

    SqlAttachmentModel::SqlAttachmentModel( QSqlDatabase db, QObject *parent)
    : QSqlRelationalTableModel(parent, db)
    setTable( "Attachment" );

    // store record to use as template for insertRecord()
    mBaseTableRecord = record();
    setRelation( eAnimalId, QSqlRelation( "animal", "uId", "earTag"));
    setRelation( eUnitId, QSqlRelation( "rumiWatchUnit", "uId", "serialNumber"));
    // get useless record with display columns fields
    mRelationalRecord = QSqlTableModel::record();


Log in to reply