QSqlRelationTableModel: specify columns for model
-
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
-
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
@infinity
While you wait for a more knowledgeable answer. I must first say that I have not actually usedQSqlRelationTableModel
, 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....-
QSqlRelationalTableModel
, andQSqlTableModel
, are table models. This means they should each refer to an actual table. What you show would be the result of aSELECT ... JOIN ...
query, and is not a table. -
The example does not explain it absolutely clearly, but the intention is that
employee
table 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
city.name
orcountry.name
in theemployees
table. 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 twoQSqlRelation
s the example creates) instead of the ids.
-