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. Having trouble with setRelation
Forum Updated to NodeBB v4.3 + New Features

Having trouble with setRelation

Scheduled Pinned Locked Moved Solved General and Desktop
11 Posts 4 Posters 1.8k Views 2 Watching
  • 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.
  • ? Offline
    ? Offline
    A Former User
    wrote on last edited by
    #1

    I have a database with a few tables. I'm trying to use two of the tables in a relational setting. Here are the queries that create the tables:

    query = "CREATE TABLE IF NOT EXISTS recipe_names("
                "name_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                "r_name VARCHAR(35));";
    
    query1 ="CREATE TABLE IF NOT EXISTS recipe_ingredients("
                "ingredient_id INTEGER PRIMARY KEY AUTOINCREMENT, "
                "name_id INTEGER,"
                "ingredient VARCHAR(35),"
                "amount FLOAT);";
    

    And this is the code I'm using, code I got from a youtube video (around 6 mins into the vid)

    model = new QSqlRelationalTableModel(this);
    model->setTable("recipe_names");
    model->setRelation(0,QSqlRelation("recipe_ingredients","name_id","name"));
    model->select();
    

    I've also viewed the Qt Docs concerning relational tables, but I just don't get it. Setting this up in Delphi is a piece of cake. Not so much with Qt.

    Using my two tables, can someone help me? Thanks.

    1 Reply Last reply
    0
    • mrjjM Offline
      mrjjM Offline
      mrjj
      Lifetime Qt Champion
      wrote on last edited by
      #2

      Hi
      So it says that
      recipe_names col 0 is key to recipe_ingredients mapped via
      name_id and should show recipe_ingredients "name" col.
      But i dont see it has a name col.
      Even if i read it reversed, there is only r_name ?

      1 Reply Last reply
      0
      • ? Offline
        ? Offline
        A Former User
        wrote on last edited by
        #3

        I changed

        model->setRelation(0,QSqlRelation("recipe_ingredients","name_id","name"));
        

        to

        model->setRelation(0,QSqlRelation("recipe_ingredients","name_id","ingredients"));
        

        I get the same thing. A blank TableView. I don't understand what I'm supposed to be doing. I use a lot of tables in my programming, most of them relationally connected. I use Delphi and Lazarus for this. And it's simple to do. But the way to do the same in Qt is strange. I just don't get it.

        1 Reply Last reply
        0
        • KillerSmathK Offline
          KillerSmathK Offline
          KillerSmath
          wrote on last edited by KillerSmath
          #4

          @landslyde

          I changed
          model->setRelation(0,QSqlRelation("recipe_ingredients","name_id","name"));
          to
          model->setRelation(0,QSqlRelation("recipe_ingredients","name_id","ingredients"));

          Replace ingredients by ingredient, because "ingredients" is not a column of recipe_ingredients.

          @Computer Science Student - Brazil
          Web Developer and Researcher
          “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

          1 Reply Last reply
          0
          • ? Offline
            ? Offline
            A Former User
            wrote on last edited by A Former User
            #5

            Yeah. I was in a hurry, late for a doctor appt when I put that in. However, it changed nothing. I hope the pic I'm putting in here shows up.
            alt text
            I tried several combinations last night before going to sleep. I can't find anything that Qt accepts to make this work.

            I embedded a pic here, but it doesn't show. Here's the link to it: https://imgur.com/qEFn8On

            1 Reply Last reply
            0
            • SGaistS Offline
              SGaistS Offline
              SGaist
              Lifetime Qt Champion
              wrote on last edited by
              #6

              Hi,

              Your database path is relative, Therefore, unless you copied the database file in the build folder, your application will just create a new empty database. This has nothing to do with Qt, it is how SQLite works.

              Interested in AI ? www.idiap.ch
              Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

              1 Reply Last reply
              3
              • ? Offline
                ? Offline
                A Former User
                wrote on last edited by
                #7

                That's embarrassing. Thanks, SGaist.

                I copied the same-nameed db over to the debug folder where this project is and it works as expected. Thank you.

                1 Reply Last reply
                0
                • KillerSmathK Offline
                  KillerSmathK Offline
                  KillerSmath
                  wrote on last edited by
                  #8

                  @SGaist mentioned

                  Your database path is relative, Therefore, unless you copied the database file in the build folder, your application will just create a new empty database.

                  @landslyde, you could use the path using the relative path of your application, similar:

                  #include <QCoreApplication>
                  ...
                  db.setDatabaseName(QCoreApplication::applicationDirPath()+"/zyx.db");
                  

                  @Computer Science Student - Brazil
                  Web Developer and Researcher
                  “Sometimes it’s the people no one imagines anything of who do the things that no one can imagine.” - Alan Turing

                  1 Reply Last reply
                  1
                  • ? Offline
                    ? Offline
                    A Former User
                    wrote on last edited by
                    #9

                    Thanks for the help, Forum.

                    1 Reply Last reply
                    0
                    • SGaistS Offline
                      SGaistS Offline
                      SGaist
                      Lifetime Qt Champion
                      wrote on last edited by SGaist
                      #10

                      QStandardPaths is also worth taking a look at.

                      Interested in AI ? www.idiap.ch
                      Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                      1 Reply Last reply
                      0
                      • ? Offline
                        ? Offline
                        A Former User
                        wrote on last edited by
                        #11

                        @SGaist - good information. Thank you.

                        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