Important: Please read the Qt Code of Conduct -

QSqlRelationTableModel: specify columns for model

  • If I use QSqlRelationalTableModel as described in the docs 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,,,,

  • @infinity
    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....

    • QSqlRelationalTableModel, and 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 employee table will have the following columns (in this order):

    city_id    /* FK to a ``, column #2 used in the example's `setRelation()` */
    country_id    /* FK to a ``, column #3 used in the example's `setRelation()` */
    • You will not have columns like or in the employees 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 two QSqlRelations the example creates) instead of the ids.

Log in to reply