Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Pyside6 - QSqlRelationalTableModel issue updating a row.



  • Hi.

    What I did:

    I want to display and edit an sqlite table using QTableView and QSqlRelationalTableModel. Some of the columns in the model has relation with other tables, which I set by calling:

    model.setRelation(index, QSqlRelation("other_table_name", "col_name", "col_name"))

    I then try to display comboboxes for these columns by:

    self.table_view.setItemDelegate(QSqlRelationalDelegate(self.table_view))

    What works:

    I am able to see the existing sqlite tables. The combobox works as expected. I am able to insert a new row when I call submitAll()

    The problem:

    I am not able to edit an existing row. I can see that that the name of columns for which I called setRelation() have changed automatically, and I guess it related to this note I found here:

    https://doc.qt.io/qtforpython/PySide6/QtSql/QSqlRelationalTableModel.html#PySide6.QtSql.PySide6.QtSql.QSqlRelationalTableModel.setRelation

    "If a relation's display column name is also used as a column name in the relational table, or if it is used as display column name in more than one relation it will be aliased. The alias is the relation's table name, display column name and a unique id joined by an underscore (e.g. tablename_columnname_id). fieldName() will return the aliased column name. All occurrences of the duplicate display column name are aliased when duplication is detected, but no aliasing is done to the column names in the main table. The aliasing doesn't affect QSqlRelation , so will return the original display column name."

    This is the error that I get when I edit the row and submitAll() :

    no such column: input_variables.structures_name_3 Unable to execute statement
    

    structures_name_3 is the alias created as mentioned in the note, the actual name of the column was structure which was referring to table structures, column name.

    Can anyone help me how to fix this issue? What should I do if I want to edit an existing row?



  • I found the solution here: https://forum.qt.io/topic/75274/qsqlrelationaltablemodel-does-not-update/2
    My table did not have a primary key so I added a column id integer primary key which fixed the problem.



  • I found the solution here: https://forum.qt.io/topic/75274/qsqlrelationaltablemodel-does-not-update/2
    My table did not have a primary key so I added a column id integer primary key which fixed the problem.


Log in to reply