QSqlRelationalTableModel issue updating a row

Heads up! You've already completed this tutorial.

tobythomas10247 | 2021-03-17 19:20:54 UTC | #1

Hi.

Summary of what I have done:

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

Over 10,000 developers have bought Create GUI Applications with Python & Qt!
Create GUI Applications with Python & Qt5
Take a look

Downloadable ebook (PDF, ePub) & Complete Source code

Also available from Leanpub and Amazon Paperback

[[ discount.discount_pc ]]% OFF for the next [[ discount.duration ]] [[discount.description ]] with the code [[ discount.coupon_code ]]

Purchasing Power Parity

Developers in [[ country ]] get [[ discount.discount_pc ]]% OFF on all books & courses with code [[ discount.coupon_code ]]

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

Summary of my 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."

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


Well done, you've finished this tutorial! Mark As Complete
[[ user.completed.length ]] completed [[ user.streak+1 ]] day streak

QSqlRelationalTableModel issue updating a row was written by Martin Fitzpatrick .

Martin Fitzpatrick has been developing Python/Qt apps for 8 years. Building desktop applications to make data-analysis tools more user-friendly, Python was the obvious choice. Starting with Tk, later moving to wxWidgets and finally adopting PyQt.