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. Updating a relational table view

Updating a relational table view

Scheduled Pinned Locked Moved Unsolved General and Desktop
4 Posts 3 Posters 1.2k 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.
  • Dan3460D Offline
    Dan3460D Offline
    Dan3460
    wrote on last edited by
    #1

    I made this simple dialog to show the problem that i'm having, the dialog is just a QTableView and QSqlRelationalTableModel. The problem is that i cannot save the view, even when the data changed was not any of the related tables. But if i comment out the relations, as in the code below, i can update with no problem. Is there an extra step to save information when there are foreign keys in play?

    PS. Just in case is important I'm running a MySql server across the network.

    #include "xtest.h"
    #include "ui_xtest.h"
    
    XTest::XTest(QWidget *parent) : QDialog(parent), ui(new Ui::XTest)
    {
      ui->setupUi(this);
      modelSuppliers=new QSqlRelationalTableModel(this);
      modelSuppliers->setTable("supplier");
      modelSuppliers->setEditStrategy(QSqlTableModel::OnManualSubmit);
      modelSuppliers->setHeaderData(0,Qt::Horizontal,"ID");
      modelSuppliers->setHeaderData(1,Qt::Horizontal,"Name");
      modelSuppliers->setHeaderData(2,Qt::Horizontal,"Address");
      modelSuppliers->setHeaderData(3,Qt::Horizontal,"Address1");
      modelSuppliers->setHeaderData(4,Qt::Horizontal,"City");
      modelSuppliers->setHeaderData(5,Qt::Horizontal,"User");
      modelSuppliers->setHeaderData(6,Qt::Horizontal,"Country");
      modelSuppliers->setHeaderData(7,Qt::Horizontal,"Sta/Reg");
      modelSuppliers->setHeaderData(8,Qt::Horizontal,"Active");
    //  modelSuppliers->setRelation(5,QSqlRelation("user","iduser","email"));
    //  modelSuppliers->setRelation(6,QSqlRelation("country","idcountry","name"));
    //  modelSuppliers->setRelation(7,QSqlRelation("sta_reg","idsta_reg","name"));
      modelSuppliers->select();
    
      ui->suppliers->setModel(modelSuppliers);
      ui->suppliers->resizeColumnsToContents();
      ui->suppliers->resizeRowsToContents();
      ui->suppliers->setItemDelegate(new QSqlRelationalDelegate(ui->suppliers));
      ui->suppliers->show();
    }
    
    XTest::~XTest()
    {
      delete ui;
    }
    
    void XTest::on_save_clicked()
    {
      modelSuppliers->submitAll();
    }
    

    I've been playing with this for a while and i have discovered something that may be related. I connected the signal beforeUpdate of the model to a function that displays the row and QSqlRecord. I made two runs one with the relation "user" active and another with non of them active. On line #5 it seems to me that the model is trying to update the record with the description of the related table and not with the key. The second run, the one that updates, has the key on that position. Is that a bug on QT?

    2
    QSqlRecord(9)
     0: QSqlField("idsupplier", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: true, readOnly: false) "4" 
     1: QSqlField("name", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Other" 
     2: QSqlField("address", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Direccion" 
     3: QSqlField("address1", QString, length: 180, precision: 0, required: no, generated: yes, typeID: 253, autoValue: false, readOnly: false) "dddd" 
     4: QSqlField("city", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Santiago" 
     5: QSqlField("iduser", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "N/A" 
     6: QSqlField("idcountry", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "2" 
     7: QSqlField("idsta_reg", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "5" 
     8: QSqlField("active", char, length: 4, precision: 0, required: no, generated: no, typeID: 1, autoValue: false, readOnly: false) "1"
    ""
    
    .
        2
        QSqlRecord(9)
         0: QSqlField("idsupplier", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: true, readOnly: false) "4" 
         1: QSqlField("name", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Other" 
         2: QSqlField("address", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Direccion" 
         3: QSqlField("address1", QString, length: 180, precision: 0, required: no, generated: yes, typeID: 253, autoValue: false, readOnly: false) "dddd" 
         4: QSqlField("city", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Santiago" 
         5: QSqlField("iduser", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "1" 
         6: QSqlField("idcountry", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "2" 
         7: QSqlField("idsta_reg", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "5" 
         8: QSqlField("active", char, length: 4, precision: 0, required: no, generated: no, typeID: 1, autoValue: false, readOnly: false) "1"
    
    VRoninV 1 Reply Last reply
    0
    • SGaistS Offline
      SGaistS Offline
      SGaist
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi,

      Can you provide a minimal compilable example that shows the behaviour you describe ?

      Interested in AI ? www.idiap.ch
      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

      1 Reply Last reply
      0
      • Dan3460D Dan3460

        I made this simple dialog to show the problem that i'm having, the dialog is just a QTableView and QSqlRelationalTableModel. The problem is that i cannot save the view, even when the data changed was not any of the related tables. But if i comment out the relations, as in the code below, i can update with no problem. Is there an extra step to save information when there are foreign keys in play?

        PS. Just in case is important I'm running a MySql server across the network.

        #include "xtest.h"
        #include "ui_xtest.h"
        
        XTest::XTest(QWidget *parent) : QDialog(parent), ui(new Ui::XTest)
        {
          ui->setupUi(this);
          modelSuppliers=new QSqlRelationalTableModel(this);
          modelSuppliers->setTable("supplier");
          modelSuppliers->setEditStrategy(QSqlTableModel::OnManualSubmit);
          modelSuppliers->setHeaderData(0,Qt::Horizontal,"ID");
          modelSuppliers->setHeaderData(1,Qt::Horizontal,"Name");
          modelSuppliers->setHeaderData(2,Qt::Horizontal,"Address");
          modelSuppliers->setHeaderData(3,Qt::Horizontal,"Address1");
          modelSuppliers->setHeaderData(4,Qt::Horizontal,"City");
          modelSuppliers->setHeaderData(5,Qt::Horizontal,"User");
          modelSuppliers->setHeaderData(6,Qt::Horizontal,"Country");
          modelSuppliers->setHeaderData(7,Qt::Horizontal,"Sta/Reg");
          modelSuppliers->setHeaderData(8,Qt::Horizontal,"Active");
        //  modelSuppliers->setRelation(5,QSqlRelation("user","iduser","email"));
        //  modelSuppliers->setRelation(6,QSqlRelation("country","idcountry","name"));
        //  modelSuppliers->setRelation(7,QSqlRelation("sta_reg","idsta_reg","name"));
          modelSuppliers->select();
        
          ui->suppliers->setModel(modelSuppliers);
          ui->suppliers->resizeColumnsToContents();
          ui->suppliers->resizeRowsToContents();
          ui->suppliers->setItemDelegate(new QSqlRelationalDelegate(ui->suppliers));
          ui->suppliers->show();
        }
        
        XTest::~XTest()
        {
          delete ui;
        }
        
        void XTest::on_save_clicked()
        {
          modelSuppliers->submitAll();
        }
        

        I've been playing with this for a while and i have discovered something that may be related. I connected the signal beforeUpdate of the model to a function that displays the row and QSqlRecord. I made two runs one with the relation "user" active and another with non of them active. On line #5 it seems to me that the model is trying to update the record with the description of the related table and not with the key. The second run, the one that updates, has the key on that position. Is that a bug on QT?

        2
        QSqlRecord(9)
         0: QSqlField("idsupplier", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: true, readOnly: false) "4" 
         1: QSqlField("name", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Other" 
         2: QSqlField("address", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Direccion" 
         3: QSqlField("address1", QString, length: 180, precision: 0, required: no, generated: yes, typeID: 253, autoValue: false, readOnly: false) "dddd" 
         4: QSqlField("city", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Santiago" 
         5: QSqlField("iduser", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "N/A" 
         6: QSqlField("idcountry", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "2" 
         7: QSqlField("idsta_reg", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "5" 
         8: QSqlField("active", char, length: 4, precision: 0, required: no, generated: no, typeID: 1, autoValue: false, readOnly: false) "1"
        ""
        
        .
            2
            QSqlRecord(9)
             0: QSqlField("idsupplier", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: true, readOnly: false) "4" 
             1: QSqlField("name", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Other" 
             2: QSqlField("address", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Direccion" 
             3: QSqlField("address1", QString, length: 180, precision: 0, required: no, generated: yes, typeID: 253, autoValue: false, readOnly: false) "dddd" 
             4: QSqlField("city", QString, length: 180, precision: 0, required: no, generated: no, typeID: 253, autoValue: false, readOnly: false) "Santiago" 
             5: QSqlField("iduser", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "1" 
             6: QSqlField("idcountry", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "2" 
             7: QSqlField("idsta_reg", int, length: 11, precision: 0, required: yes, generated: no, typeID: 3, autoValue: false, readOnly: false) "5" 
             8: QSqlField("active", char, length: 4, precision: 0, required: no, generated: no, typeID: 1, autoValue: false, readOnly: false) "1"
        
        VRoninV Offline
        VRoninV Offline
        VRonin
        wrote on last edited by
        #3

        @Dan3460 said in Updating a relational table view:

        On line #5 it seems to me that the model is trying to update the record with the description of the related table and not with the key. The second run, the one that updates, has the key on that position. Is that a bug on QT?

        not a bug it is the correct behaviour. if you see here you notice the first run is just to update how the view displays it while the second is what gets uploaded to the database.

        Can you change

        void XTest::on_save_clicked()
        {
          modelSuppliers->submitAll();
        }
        

        into

        void XTest::on_save_clicked()
        {
          if(!modelSuppliers->submitAll())
        qDebug() << modelSuppliers->lastError()->text();
        }
        

        and tell us what the output is?

        "La mort n'est rien, mais vivre vaincu et sans gloire, c'est mourir tous les jours"
        ~Napoleon Bonaparte

        On a crusade to banish setIndexWidget() from the holy land of Qt

        1 Reply Last reply
        0
        • Dan3460D Offline
          Dan3460D Offline
          Dan3460
          wrote on last edited by
          #4

          Thanks every one for the replays, i have decided to use regular SQL statements to manipulate the database, i have more control and i think is simpler to use. I'm using QSqlRelationalTableModel to display but updates, inserts and deletes are done in SQL.

          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