Results 1 to 3 of 3

Thread: QSqlRelationalTableModel + QSqlRelation Failing when some fields are null

  1. #1
    Join Date
    Apr 2011
    Posts
    4
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSqlRelationalTableModel + QSqlRelation Failing when some fields are null

    Hi all

    I'm using a QSQlRelationalTableModel with QSqlRelation as per the 4.7 documentation.
    However if just one of the rows contains data in a column referred to by a QSqlRelation that doesn't map to a valid foreign key (e.g NULL), the model stops working - at least the Tableview displaying it shows no data.
    For example:

    Qt Code:
    1. Tables:
    2.  
    3. CREATE TABLE MaterialProperty (
    4. ID text UNIQUE PRIMARY KEY NOT NULL,
    5. ParameterID text,
    6. ScalarValue real,
    7. CurveID text,
    8. StringValue text,
    9. UnitID text,
    10. MaterialID text,
    11. FieldID text,
    12. VersionNumber integer,
    13. VersionDate datetime,
    14. /* Foreign keys */
    15. FOREIGN KEY (CurveID)
    16. REFERENCES SeriesIndex(ID)
    17. ON DELETE NO ACTION
    18. ON UPDATE NO ACTION,
    19. FOREIGN KEY (UnitID)
    20. REFERENCES Unit(ID)
    21. ON DELETE NO ACTION
    22. ON UPDATE NO ACTION,
    23. FOREIGN KEY (MaterialID)
    24. REFERENCES MaterialIndex(ID)
    25. ON DELETE NO ACTION
    26. ON UPDATE NO ACTION,
    27. FOREIGN KEY (ParameterID)
    28. REFERENCES Parameter(ID)
    29. ON DELETE NO ACTION
    30. ON UPDATE NO ACTION,
    31. FOREIGN KEY (FieldID)
    32. REFERENCES FieldIndex(ID)
    33. ON DELETE NO ACTION
    34. ON UPDATE NO ACTION
    35. );
    36.  
    37. CREATE TABLE SeriesIndex (
    38. ID text UNIQUE PRIMARY KEY NOT NULL,
    39. Name varchar(50),
    40. Title varchar(50),
    41. Dims integer default 2,
    42. Xlabel varchar(50) default "X",
    43. Ylabel varchar(50) default "Y",
    44. Zlabel varchar(50) default "Z",
    45. VersionNumber integer,
    46. VersionDate datetime
    47. );
    48.  
    49. QSqlRelationalTableModel *model = db->get_db_model();
    50. model->setTable("materialproperty");
    51. QSqlRelation r("seriesindex", "id", "name");
    52. if(r.isValid()) // this passes
    53. model->setRelation(3, r);
    To copy to clipboard, switch view to plain text mode 

    If a row in materialproperty has no curveID, then no data is presented in the view at all. If I don't set up the relation, all appears as expected. The same if any other foreign key value is NULL.
    I have a number of optional parameters such as curveID that can end up null.

    Is there a way I can stop this behaviour? I don't see why a failure to map a NULL value to a foreign key should break the whole view/model (i'm not sure which is broken exactly, I presume it's the model). FWIW I am using an Sqlite database.

    I would prefer not to have to fill these columns with data (i.e some foreign key to represent null) simply to get Qt to work with them.

    Your help most appreciated - I've asked this over at stack overflow but haven't had a single response.

    before you ask I am using UUID's as keys to allow data migration between databases, hence the text primary key values.
    Last edited by kreyszig; 26th April 2011 at 11:58. Reason: added schema info

  2. #2
    Join Date
    Jun 2012
    Posts
    219
    Thanks
    28
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlRelationalTableModel + QSqlRelation Failing when some fields are null

    I have the same issue and would be interested in a work-around.

  3. #3
    Join Date
    Oct 2014
    Posts
    22
    Qt products
    Qt3 Qt4 Qt5 PyQt3 PyQt4

    Default Re: QSqlRelationalTableModel + QSqlRelation Failing when some fields are null

    I know this thread has been dead awhile. Is there a solution to this? I am running into the same issue as well.


    Added after 1 28 minutes:


    The answer is self.setJoinMode(QtSql.QSqlRelationalTableModel.Le ftJoin).

    QSqlRelationalTableModel.setJoinMode (self, JoinMode joinMode)
    Sets the SQL join mode to the value given by joinMode to show or hide rows with NULL foreign keys.
    In InnerJoin mode (the default) these rows will not be shown; use the LeftJoin mode if you want to show them.
    This function was introduced in Qt 4.8.
    Last edited by lukeQt; 14th April 2015 at 22:19.

Similar Threads

  1. Refresh QSqlRelation
    By Banjo in forum Qt Programming
    Replies: 3
    Last Post: 23rd May 2018, 10:39
  2. Shutdown on Mac failing
    By ntp in forum Qt Programming
    Replies: 4
    Last Post: 11th December 2010, 01:41
  3. [QSqlRelation] Is this possible?
    By mhbeyle in forum Qt Programming
    Replies: 3
    Last Post: 29th November 2009, 19:14
  4. Replies: 2
    Last Post: 22nd January 2009, 12:29
  5. Trouble with QSqlRelationalTableModel and QSqlRelation
    By yleesun in forum Qt Programming
    Replies: 0
    Last Post: 8th August 2008, 10:16

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.