Results 1 to 7 of 7

Thread: Method to read foreign key values from QSqlRelationalTableModel ?

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Posts
    47
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Method to read foreign key values from QSqlRelationalTableModel ?

    Agreed. This is certainly an option. Along the same thinking I could also implement my original suggestion of an additional flag, such as Qt::RawDisplayData.

    Looking at the source code for QSqlRelationalTableModel, I realize why an indirection (i.e., getting related value first, then look up foreign key) is needed: behind the scene QSqlrelationalTableModel builds an SQL statement with joins and omitting the raw foreign keys.

  2. #2
    Join Date
    Jun 2011
    Location
    Białystok, Poland
    Posts
    13
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows
    Thanked 1 Time in 1 Post

    Default Re: Method to read foreign key values from QSqlRelationalTableModel ?

    Old thread but I was looking for solution. Here is what You should do:

    model->setRelation(model->fieldIndex("continent_id"), QSqlRelation("Table_2", "continent_id", "continent_name, continent_id as continent_id"));

    or something like that. I have table Orders with contractor_id and Contractors with id so I had to use:

    setRelation(this->fieldIndex("contractor_id"), QSqlRelation("Contractors", "id", "name, contractor_id as contractor_id"));

    'displayColumn' in Qt documentation is misleading. It should be called displayColumns.

  3. The following user says thank you to 7ymekk for this useful post:

    Al_ (16th November 2013)

  4. #3
    Join Date
    Dec 2009
    Posts
    47
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android
    Thanks
    11
    Thanked 1 Time in 1 Post

    Default Re: Method to read foreign key values from QSqlRelationalTableModel ?

    An unexpected solution! Thanks!

    Providing more than one column name as third parameter of the QSqlRelation constructor is undocumented (you call it 'misleading'), thus the internal handling might change in the future. But at present your solution is very nice.

  5. #4
    Join Date
    Oct 2015
    Posts
    1
    Qt products
    Qt5
    Platforms
    MacOS X Unix/X11 Maemo/MeeGo

    Default Re: Method to read foreign key values from QSqlRelationalTableModel ?

    Sorry to bump this, but I've found another solution and I thought that someone may find it helpful :

    I've built a class inheriting from QSqlTableModel rather than QSqlRelationalTableModel and chose to override the selectStatement and orderByClause methods.
    Everything works well except that you won't be able to use setSort() if you want to order by a joined column. But since it allows you to provide your own SELECT query, it's not a real problem. setFilter and all other methods work as expected, which is very convenient

    Qt Code:
    1. QString MyClass::QUERY = QString("SELECT t1.col1, t1.col2, t1.col3, t2.col1, t2.col2 FROM table1 t1 JOIN table2 t2 ON t1.col2 = t2.col1");
    2. QString MyClass::ORDER_BY = QString("ORDER BY t2.col2 ASC");
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. QString MyClass::selectStatement() const
    2. {
    3. QString stmt = MyClass::QUERY;
    4.  
    5. // Sets filter (WHERE clause) :
    6. if(!this->filter().isEmpty())
    7. stmt.append(QLatin1String(" "))
    8. .append(QLatin1String("WHERE "))
    9. .append(this->filter());
    10.  
    11. // Sets sort (ORDER BY clause) :
    12. stmt.append(QLatin1String(" "))
    13. .append(this->orderByClause());
    14.  
    15. return stmt;
    16. }
    17.  
    18. QString MyClass::orderByClause() const
    19. {
    20. return MyClass::ORDER_BY;
    21. }
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 3
    Last Post: 26th February 2010, 23:37
  2. How to read and get the values out of the xml?
    By dark1988 in forum Qt Programming
    Replies: 1
    Last Post: 26th July 2008, 01:29
  3. How to read and get the values out of the xml?
    By dark1988 in forum Qt Programming
    Replies: 6
    Last Post: 15th July 2008, 09:41
  4. FOREIGN KEY + QComboBox
    By eleanor in forum Qt Programming
    Replies: 1
    Last Post: 8th November 2007, 11:37
  5. Replies: 8
    Last Post: 28th May 2007, 21:32

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Qt is a trademark of The Qt Company.