Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct
Using SetRelation to get a foreign key pointed to by another foreign key
reneed last edited by
I have a table for books containing the attributes of title, authno, and coauthno. Authno is the foreign key into the table authors. Coauthno is a foreign key into coauthors where each record contains the coauthno and authno. There may be one or more records for each coauthno. Coauthor.authno is a foreign key into authors. This give an author to each title and 0 or more coauthors to the title.
Displaying the author seems straight forward as shown:
tableModel = new QSqlRelationalTableModel( this ) ;
tableModel->setTable( "books" ) ;
tableModel->setRelation( 1 , QSqlRelation( "authors", "authno" , "name" ) ) ;
// this not really what i want... instead of authno, i really want name of the coauthor.
tableModel->setRelation( 2 , QSqlRelation( "coauthors", "coauthno", "authno" ) ) ;
I don't think the last line is how I can get to the coauthors' name. How can I do this?