Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlRelationTableModel: specify columns for model

QSqlRelationTableModel: specify columns for model

Scheduled Pinned Locked Moved Unsolved General and Desktop
2 Posts 2 Posters 209 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • I Offline
    I Offline
    Infinity
    wrote on last edited by Infinity
    #1

    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
    
    JonBJ 1 Reply Last reply
    0
    • I 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
      
      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by JonB
      #2

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

      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 or country.name 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.
      1 Reply Last reply
      1

      • Login

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • Users
      • Groups
      • Search
      • Get Qt Extensions
      • Unsolved