Having trouble with setRelation



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


  • Qt Champions 2017

    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 ?



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



  • @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.



  • 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


  • Lifetime Qt Champion

    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.



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



  • @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");
    


  • Thanks for the help, Forum.


  • Lifetime Qt Champion

    QStandardPaths is also worth taking a look at.



  • @SGaist - good information. Thank you.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.