Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. Reference to non-existing key in QSqlRelationalTableModel
QtWS25 Last Chance

Reference to non-existing key in QSqlRelationalTableModel

Scheduled Pinned Locked Moved Unsolved General and Desktop
sqlforeign key
6 Posts 2 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • J Offline
    J Offline
    Jendker
    wrote on last edited by
    #1

    Hello everyone,

    I am struggling since a long time to create the relatively simple use case. I am having the table of the workers set to a QSqlRelationalTableModel, where one of the fields would be the foreign key of the supervisor from the defined table relation.

    Now some works will not have any supervisor. For now I was just creating the record "no_supervisor" in supervisor table and I was assigning it to it all the workers which don't need any key, otherwise the worker's record would not be shown in the QTableView, the existing foreign key has to be given. The problem is, that now in the supervisor QTableView I will always have this dummy record "no_supervisor" so to avoid it I was just hiding the row. Then I got into problem, that when I was sorting the supervisor QTableView and I was some additional supervisors the hidden dummy element could have been shown again, because some of the records move down by. You see my point.

    So my question is, how may I just give the information such as NULL and still get the record shown in the QTableView?

    JonBJ 1 Reply Last reply
    0
    • J Jendker

      Hello everyone,

      I am struggling since a long time to create the relatively simple use case. I am having the table of the workers set to a QSqlRelationalTableModel, where one of the fields would be the foreign key of the supervisor from the defined table relation.

      Now some works will not have any supervisor. For now I was just creating the record "no_supervisor" in supervisor table and I was assigning it to it all the workers which don't need any key, otherwise the worker's record would not be shown in the QTableView, the existing foreign key has to be given. The problem is, that now in the supervisor QTableView I will always have this dummy record "no_supervisor" so to avoid it I was just hiding the row. Then I got into problem, that when I was sorting the supervisor QTableView and I was some additional supervisors the hidden dummy element could have been shown again, because some of the records move down by. You see my point.

      So my question is, how may I just give the information such as NULL and still get the record shown in the QTableView?

      JonBJ Offline
      JonBJ Offline
      JonB
      wrote on last edited by
      #2

      @Jendker
      So have you tried assigning the parent value to NULL in the child row(s) and seeing what happens? From what I read, this ought to work, the child will not show a selected value from the parent. I don't know how the interface displays this, how easy it is via the combobox in the view to swap between selecting a parent and no parent and back again, you'll have to check that out.

      J 1 Reply Last reply
      1
      • JonBJ JonB

        @Jendker
        So have you tried assigning the parent value to NULL in the child row(s) and seeing what happens? From what I read, this ought to work, the child will not show a selected value from the parent. I don't know how the interface displays this, how easy it is via the combobox in the view to swap between selecting a parent and no parent and back again, you'll have to check that out.

        J Offline
        J Offline
        Jendker
        wrote on last edited by
        #3

        @JonB Thanks, I am trying it out. For now setting NULL for the foreign key does not allow the record to be shown in the QTableView, but maybe I can make it somehow work with different widgets, for example combobox as you proposed.

        JonBJ 1 Reply Last reply
        0
        • J Jendker

          @JonB Thanks, I am trying it out. For now setting NULL for the foreign key does not allow the record to be shown in the QTableView, but maybe I can make it somehow work with different widgets, for example combobox as you proposed.

          JonBJ Offline
          JonBJ Offline
          JonB
          wrote on last edited by
          #4

          @Jendker
          [I proposed QCompleter rather than QCombBox.] Setting NULL in the child key column for the parent (worker has no supervisor) prevents display of the whole worker row when viewing the list of workers? I presumed it would show the worker but have a "blank" where the parent should be. If not, it does not look like QSqlRelationalTableModel likes NULL, it expects a genuine, existent parent for the child to be valid/displayed, which is not good for your situation....

          J 1 Reply Last reply
          1
          • JonBJ JonB

            @Jendker
            [I proposed QCompleter rather than QCombBox.] Setting NULL in the child key column for the parent (worker has no supervisor) prevents display of the whole worker row when viewing the list of workers? I presumed it would show the worker but have a "blank" where the parent should be. If not, it does not look like QSqlRelationalTableModel likes NULL, it expects a genuine, existent parent for the child to be valid/displayed, which is not good for your situation....

            J Offline
            J Offline
            Jendker
            wrote on last edited by Jendker
            #5

            @JonB said in Reference to non-existing key in QSqlRelationalTableModel:

            If not, it does not look like QSqlRelationalTableModel likes NULL, it expects a genuine, existent parent for the child to be valid/displayed, which is not good for your situation....

            It seems, that it's exactly the case... I will just try to carry on with improving the hiding of the dummy row, which will stand for "none" in order to keep the things working with QSqlRelationalTableModel

            I don't know, maybe some time I will just drop the QSqlTableModels altogether and work with the queries.

            The solution can be also in the post by wysota on the old forum back in 2009:

            "What would you like to show instead of null values in the rows that contain them? A NULL value in a field that is a foreign key can be considered a design flaw of the database. If you want to have null values as foreign key then don't pretend this is a relation and use QSqlTableModel() with a custom delegate that will treat the foreign key in a special manner - by replacing its values with ones from another table. "

            That would be some work in rebuilding database scheme, but it could be the actual solution. I will try this one first.

            JonBJ 1 Reply Last reply
            0
            • J Jendker

              @JonB said in Reference to non-existing key in QSqlRelationalTableModel:

              If not, it does not look like QSqlRelationalTableModel likes NULL, it expects a genuine, existent parent for the child to be valid/displayed, which is not good for your situation....

              It seems, that it's exactly the case... I will just try to carry on with improving the hiding of the dummy row, which will stand for "none" in order to keep the things working with QSqlRelationalTableModel

              I don't know, maybe some time I will just drop the QSqlTableModels altogether and work with the queries.

              The solution can be also in the post by wysota on the old forum back in 2009:

              "What would you like to show instead of null values in the rows that contain them? A NULL value in a field that is a foreign key can be considered a design flaw of the database. If you want to have null values as foreign key then don't pretend this is a relation and use QSqlTableModel() with a custom delegate that will treat the foreign key in a special manner - by replacing its values with ones from another table. "

              That would be some work in rebuilding database scheme, but it could be the actual solution. I will try this one first.

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @Jendker

              A NULL value in a field that is a foreign key can be considered a design flaw of the database.

              When I started to answer your question, that was my initial position (except I wouldn't say "flaw"). When I have used FKs I was never allowed NULL, and I thought that was actually required. However I Googled and plenty said many databases support it (e.g. MySQL) and it was regarded as "acceptable".

              I never did think QSqlRelationalTableModel offered much over QSqlTableModel. It's really just a glorified combobox (model being the FK table) interface in the view. Exactly as 2009 says, a custom delegate could do the job. Just a shame that Qt's doesn't offer NULL handling option for what you want.

              1 Reply Last reply
              0

              • Login

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • Users
              • Groups
              • Search
              • Get Qt Extensions
              • Unsolved