Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. How to get a record to insertRecord(-1, rec) in QSqlRelationalTableModel
Forum Updated to NodeBB v4.3 + New Features

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

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 2.1k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    SwissKnife
    wrote on last edited by
    #1

    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();
    

    }
    @

    1 Reply Last reply
    0

    • Login

    • Login or register to search.
    • First post
      Last post
    0
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Get Qt Extensions
    • Unsolved