QSqlRelationalTableModel issue updating a row


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:


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:


“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?