Results 1 to 13 of 13

Thread: Unable to perform setValue on QSqlRecord when model has relations

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,373
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Thanks
    3
    Thanked 5,019 Times in 4,795 Posts
    Wiki edits
    10

    Default Re: Unable to perform setValue on QSqlRecord when model has relations

    What does this print?

    Qt Code:
    1. for(int i=0;i<record.count();++i){
    2. qDebug() << "Field " << i << "is called" << record.fieldName(i);
    3. }
    To copy to clipboard, switch view to plain text mode 
    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.


  2. The following user says thank you to wysota for this useful post:

    stevebakh (21st March 2010)

  3. #2
    Join Date
    Feb 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Unable to perform setValue on QSqlRecord when model has relations

    Quote Originally Posted by wysota View Post
    What does this print?

    Qt Code:
    1. for(int i=0;i<record.count();++i){
    2. qDebug() << "Field " << i << "is called" << record.fieldName(i);
    3. }
    To copy to clipboard, switch view to plain text mode 
    Oooh, this gives interesting results. It turns out that the field name changes when adding the relation, it's appended with "_2", so "foo_id_2".

    Here's some code as an example:

    Qt Code:
    1. model->setTable("foo");
    2. model->setRelation(model->fieldIndex("bar_id"), QSqlRelation("bar", "id", "somecolumn"));
    3. model->select();
    4.  
    5. QSqlRecord row = model->record();
    6. // the following fails to set the value
    7. row.setValue("bar_id", 123);
    8.  
    9. // because the following returns -1
    10. qDebug() << row.indexOf("bar_id");
    11.  
    12. for (int i = 0; i < row.count(); i++) {
    13. qDebug() << "field: " << i << " is called: " << row.fieldName(i);
    14. }
    15.  
    16. // the above outputs the following:
    17. // field: 0 is called: "blah"
    18. // field: 1 is called: "blah1"
    19. // field: 2 is called: "blah2"
    20. // field: 3 is called: "bar_id_2" <<<< The _2 is appended only when a setRelation() is called on the model.
    To copy to clipboard, switch view to plain text mode 

    So, doesn't this happen if you try my example code? Is this standard / normal behaviour?

  4. #3
    Join Date
    Sep 2008
    Location
    Portugal
    Posts
    171
    Qt products
    Qt5
    Platforms
    Unix/X11 Windows
    Thanks
    57
    Thanked 4 Times in 4 Posts

    Default Re: Unable to perform setValue on QSqlRecord when model has relations

    for(int i=0;i<record.count();++i){
    qDebug() << "Field " << i << "is called" << record.fieldName(i);
    }
    I tested this code and, for the foreign key field, i get: REFERENCEDTABLENAME_REFERENCEDFIELDNAME.
    Nothing strange here!?

    In you exemple the filed name show be "bar_somecolumn".

  5. The following user says thank you to graciano for this useful post:

    stevebakh (22nd March 2010)

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

    Default Re: Unable to perform setValue on QSqlRecord when model has relations

    Anyway if you referenced the column by its index and not by its name, you wouldn't have even noticed there was a name change
    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.


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

    stevebakh (22nd March 2010)

  8. #5
    Join Date
    Feb 2010
    Posts
    17
    Qt products
    Qt4
    Platforms
    Unix/X11
    Thanks
    11

    Default Re: Unable to perform setValue on QSqlRecord when model has relations

    Quote Originally Posted by wysota View Post
    Anyway if you referenced the column by its index and not by its name, you wouldn't have even noticed there was a name change
    That's very true, however, I figured it would be bad practice to just use magic numbers to refer to arbitrary columns. What if the table structure changes?

    I could define some enum values that represent each column and use those, but it's the same problem. If the table structure changes, the app breaks and needs recompiling. Hmmm, I just thought about it, I could/should probably use the model instead of the record to find the column number. record.setValue(model.fieldIndex("foo_id"), 123); Le sigh...

    Thanks guys! Lesson learnt, knowledge earnt.

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

    Default Re: Unable to perform setValue on QSqlRecord when model has relations

    Quote Originally Posted by stevebakh View Post
    That's very true, however, I figured it would be bad practice to just use magic numbers to refer to arbitrary columns. What if the table structure changes?
    I don't think it would change behind your back. If it did, the change could as well be that a column is removed at all. So we can assume that any change like that would only require you to renumber some enum and rebuild the app.
    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.


Similar Threads

  1. Unable to modify QSqlRecord in QSqlRelationalTableModel
    By pippo42 in forum Qt Programming
    Replies: 1
    Last Post: 20th March 2010, 23:17
  2. Problem in setValue for QAccessibleWidget
    By Rakesh_Kumar in forum Qt Programming
    Replies: 0
    Last Post: 29th January 2009, 09:36
  3. QSqlRecord setValue doesn't set the value
    By sgmurphy19 in forum Qt Programming
    Replies: 1
    Last Post: 7th February 2008, 20:20
  4. SetValue
    By phillip_Qt in forum Qt Programming
    Replies: 4
    Last Post: 3rd October 2007, 22:45
  5. QProgressDialog::setValue crash( Mac )
    By marcel in forum Qt Programming
    Replies: 7
    Last Post: 11th April 2007, 23:12

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.