Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
QSqlRelationTableModel: specify columns for model
Infinity last edited by Infinity
If I use QSqlRelationalTableModel as described in the docs https://doc.qt.io/qt-5/qsqlrelationaltablemodel.html#details the id of city and country are not in the model anymore. How can I achieve that the id's will remain in the model. My goal would be to have the following columns in the the model:
id, name, city.id, city.name, country.id, country.name
JonB last edited by JonB
While you wait for a more knowledgeable answer. I must first say that I have not actually used
QSqlRelationTableModel, though I have often looked at it; I have used SQL foreign keys outside of Qt. So I am not sure of my ground in Qt, you may know better than I, but....
QSqlTableModel, are table models. This means they should each refer to an actual table. What you show would be the result of a
SELECT ... JOIN ...query, and is not a table.
The example does not explain it absolutely clearly, but the intention is that
employeetable will have the following columns (in this order):
id name city_id /* FK to a `city.id`, column #2 used in the example's `setRelation()` */ country_id /* FK to a `country.id`, column #3 used in the example's `setRelation()` */
- You will not have columns like
employeestable. Only the FK ids, that's part of the point of FKs. If you need the city/country name, you will access that via the relation you have set up, which will look up the other city/country fields via the FK (e.g.
QSqlTableModel *QSqlRelationalTableModel::relationModel(int column) const,
QSqlRelation QSqlRelationalTableModel::relation(int column) const). That's what the view does to present the city/country name (as a result of the two
QSqlRelations the example creates) instead of the ids.