Results 1 to 13 of 13

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

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

    Default Unable to perform setValue on QSqlRecord when model has relations

    Hi,

    I have a QSqlRelationalTableModel model and I'm trying to create a record. So I do model->record() to get a blank record, however, if I attempt to do record.setValue("field", "value") on a column that has a relation, it fails (or rather, no value is set). If I remove the relation from the table model, it works fine. Any ideas?

    Thanks!

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

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

    Did you check if the "value" used exists in the referenced table, where i suppose "field" is the PK?
    I experienced similar problems because of this detail.

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

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

    Hi Graciano,

    I did check that the value existed in the related table before trying to set it. I didn't think it would matter anyway (not until attempting to commit the data to the database at least), but still, no joy.

    Here's an example of what I'm doing:

    Qt Code:
    1. model->setTable("foo");
    2. model->setRelation(model->fieldIndex("bar_id"),
    3. QSqlRelation("bar", "id", "bazcolumn"));
    4.  
    5. // fetch a new, empty record
    6. QSqlRecord row = model->record();
    7.  
    8. // The following fails to set 123 for the bar_id column. If I comment
    9. // out the call to setRelation() above, this works fine. Assume that
    10. // 123 is a valid id that exists in the bar table.
    11. row.setValue("bar_id", 123);
    To copy to clipboard, switch view to plain text mode 

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

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

    Well ... it's hard to say without looking at the whole thing.
    Here is a similar example i'm working on ( http://www.box.net/shared/f511adk6y3 )
    You find your line of code in line 54 of the cidadaodialog.cpp file.
    Important note: I'm using MySql Workbench to manage the database (mva1.mwb)

  5. #5
    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: Unable to perform setValue on QSqlRecord when model has relations

    As far as I remember for relation fields you have to use the foreign key (id) instead of values from the related table when changing the record.
    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.


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

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

    Quote Originally Posted by wysota View Post
    As far as I remember for relation fields you have to use the foreign key (id) instead of values from the related table when changing the record.
    Hi, could you elaborate on this point please?

    I've tried to set a value that matches the foreign key value, but I'm guessing that's not what you meant? Is there some other way of doing this?

    Also, Graciano, thanks for sharing some code, I'll take a look at it now!

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

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

    I've managed to narrow the problem down some more...

    The reason no data is set is because the column seemingly doesn't exist in the QSqlRecord when there's a relation applied to it.

    Qt Code:
    1. // when the relation isn't set, the following prints 4
    2. // if a relation IS set, it prints -1
    3. qDebug() << record.indexOf("bar_id");
    To copy to clipboard, switch view to plain text mode 

    It's simply not added to the list of fields, for whatever reason.

  8. #8
    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: 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.


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

    stevebakh (21st March 2010)

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

    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?

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

    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".

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

    stevebakh (22nd March 2010)

  13. #11
    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: 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.


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

    stevebakh (22nd March 2010)

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

    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.

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