Results 1 to 4 of 4

Thread: [QSqlRelation] Is this possible?

  1. #1
    Join Date
    Nov 2009
    Posts
    15

    Default [QSqlRelation] Is this possible?

    I have a model under QSqlRelationalTableModel that joins two tables. Primary table has an Id that point two a primary key in the secondary table. Secondary table has two fields that fill a name (name & surname).
    When I set the relation I have this QSqlRelation definition:

    QSqlRelation::QSqlRelation ( const QString & tableName, const QString & indexColumn, const QString & displayColumn )

    With this I can only add one field to the model when I need two fields. Is possible make a model based on QSqlRelationalTableModel that returns two fields for an unique Id?

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: [QSqlRelation] Is this possible?

    Each relation substitutes a foreign key with a value from another table. You can have as many relations in the table as many foreign keys. Thus if you have one foreign key, you can "import" only one field. In your case it would probably be best to make a view in the database out of those two tables or to use QSqlQueryModel.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Nov 2009
    Posts
    15

    Default Re: [QSqlRelation] Is this possible?

    Quote Originally Posted by wysota View Post
    Each relation substitutes a foreign key with a value from another table. You can have as many relations in the table as many foreign keys. Thus if you have one foreign key, you can "import" only one field. In your case it would probably be best to make a view in the database out of those two tables or to use QSqlQueryModel.
    Yes. QSqlQueryModel is the best solution for relations one-to-many but this model is read-only and I need to write extra code that is not necessary with RelationalTableModel.
    I was longing a solution inside the QSqlRelationalTableModel component but I believe that I will have to write more lines of code.

    Thanks.

  4. #4
    Join Date
    Oct 2009
    Posts
    364
    Thanks
    10
    Thanked 37 Times in 36 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: [QSqlRelation] Is this possible?

    I ran into the same issue and ended up using QSqlQuery like this:

    Qt Code:
    1.  
    2. q = db.exec("SELECT a.ID, a.name, a.surname FROM addresses as a WHERE a.ID=" + your_id);
    3.  
    4. q.next();
    5. thename = q.value(1).toString();
    6. thesurname = q.value(2).toString();
    To copy to clipboard, switch view to plain text mode 

    I haven't tested this code, but I'm sure you get the idea.
    The various sql models are great for certain scenarios but I find myself often using QSqlQuery in addition to the model, especially when the model is read-only.

    hope this helps.

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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.