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. QSqlRelationalModel Editable Issue
Forum Updated to NodeBB v4.3 + New Features

QSqlRelationalModel Editable Issue

Scheduled Pinned Locked Moved Solved General and Desktop
2 Posts 1 Posters 152 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.
  • Z Offline
    Z Offline
    ZNohre
    wrote last edited by
    #1

    All,

    I'm having an issue with a QSqlRelationalTableModel that I upgraded from a QSqlTableModel to get foreign key support.

    m_model->setRelation(1, QSqlRelation("Insured", "ID", "Name"));
    

    1ab86b11-b627-4ea5-ad26-62ca7fcbcd2a-image.png

    The code above works and my view is accurately displaying the Name field. The issue arises when I try to add a new record to my table. I'm now getting the below error message:

    qt.sql.odbc: "QODBCResult::exec: Unable to execute statement:" 	Error: "[Microsoft][ODBC SQL Server Driver][SQL Server]Invalid column name 'Name'. [Microsoft][ODBC SQL Server Driver][SQL Server]Statement(s) could not be prepared."
    

    Adding a qDebug statement in the InsertRowIntoTable(const QSqlRecord &values) method I see there is now a "Name" field for the relational table in the record which seems odd. The documentation notes that editable models are supported out of the box so I must be missing something.

    QSqlRecord(25)
     0: QSqlField("ID", int, tableName: "Policies", length: 10, required: yes, generated: no, autoValue: true, readOnly: false) "0" 
     1: QSqlField("Name", QString, tableName: "Insureds", length: 255, required: no, generated: yes, autoValue: false, readOnly: false) "TES0010"** 
     2: QSqlField("Status", QString, tableName: "Policies", length: 255, required: no, generated: yes, autoValue: false, readOnly: false) "Estimated" 
    ...
    
    1 Reply Last reply
    0
    • Z Offline
      Z Offline
      ZNohre
      wrote last edited by
      #2

      Ah, found this issue right after posting while doing some more thorough review of my overridden methods. Hopefully it helps someone else.

      For those converting QSqlTableModels to QSqlRelationalTableModels be careful what methods you're calling!

      In the below code I forgot to update the base function call to QSqlRelationalTableModel so it was still calling QSqlTableModel::insertRowIntoTable(const QSqlRecord &values) which led to these issues.

      bool PolicyModel::insertRowIntoTable(const QSqlRecord &values)
      {
          QSqlRecord rec = values;
          rec.setGenerated(Policy::DataRole::Id, false);
          return QSqlRelationalTableModel::insertRowIntoTable(rec);
      }
      

      I had overridden this method because the primary key is an AUTO_INCREMENT field.

      1 Reply Last reply
      0
      • Z ZNohre has marked this topic as solved

      • Login

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