Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Pyside6 - QSqlRelationalTableModel issue updating a row.
Forum Updated to NodeBB v4.3 + New Features

Pyside6 - QSqlRelationalTableModel issue updating a row.

Scheduled Pinned Locked Moved Solved Qt for Python
2 Posts 1 Posters 537 Views
  • 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.
  • T Offline
    T Offline
    TobyThomas
    wrote on last edited by TobyThomas
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      TobyThomas
      wrote on last edited by
      #2

      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.

      1 Reply Last reply
      1

      • Login

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