PDA

View Full Version : QSqlRelationalTableModel & advanced relationships



zuck
4th August 2009, 10:51
Hi guys!

I need to map database "many-to-many" and "one-to-many" relations into my Qt code. I didn't found an "official" Qt class for this feature so my question is: what's the best approach, in your opinion, for implementing a complete database-relationship support?

My idea is to create two new classes:

1) QSqlAdvancedRelation which inherits from QSqlRelation and maps all three main relation types ("one-to-one", "one-to-many", "many-to-many").

2) QSqlAdvancedRelationalTableModel which inherits from QSqlRelationalTableModel and uses one or more instances of QSqlAdvancedRelation class.

For many-to-many relation, the idea is to use the relation table name as a virtual field name in the original table which value is a list of QModelIndex of the related table and vice versa:


Original table <- Relationship table -> Related table

For one-to-many relation, the approach is simpler: the relation returns the list of QModelIndex of the related model filtered by a where clause.


Original table <- Related table

What do you think about this approach?

Walter
4th August 2009, 11:01
I have the same problem (http://www.qtcentre.org/forum/f-qt-programming-2/t-qsqltablemodel-qtableview-and-complex-queries-22922.html)...

zuck
4th August 2009, 11:37
Well, what about to start a collaborative project to implement a better support for database mapping in Qt? :)

Walter
5th August 2009, 19:45
Too late - I've already finalized my own QSqlQuery subclass but in a way that is very specific to my needs.

zuck
6th August 2009, 10:00
Ok :) Could you post any tips about your solution, please?

Walter
6th August 2009, 10:39
I've updated my own thread:
http://www.qtcentre.org/forum/f-qt-programming-2/t-qsqltablemodel-qtableview-and-complex-queries-22922-post112407.html