PDA

View Full Version : How to create an One-to-Many Relationship in QtSqlRelation ?



innobleday
12th March 2010, 10:00
Hi All,
I've a problem when I aim to view data into QTableView. I aim to create an One-to-Many (OTM) Relationship in QtSqlRelation. the code can I do is one-to-one like this, I've no Idea for the OTM. How should it be ?.

model->setTable("Users");
model->setSort(User_Id, Qt::AscendingOrder);
model->setHeaderData(User_Name,Qt::Horizontal,tr("Username"));
model->setHeaderData(User_FirstName, Qt::Horizontal, tr("First name"));
model->setHeaderData(User_LastName, Qt::Horizontal, tr("Last Name"));
model->setRelation(User_Mode,QSqlRelation("UserModes","ID","Name"));
model->setHeaderData(User_Mode, Qt::Horizontal, tr("Type"));
model->select();

Thanks

Hary

wysota
13th March 2010, 08:44
The code above doesn't determine if the relation between Users and UserModes is 1-1 or 1-n. You may have several users with the same user mode (1-n) with the code above.

innobleday
13th March 2010, 16:18
The code above doesn't determine if the relation between Users and UserModes is 1-1 or 1-n. You may have several users with the same user mode (1-n) with the code above.

Firstly I'm not sure with my code.. Sorry if I miss understanding about 1-n. But the problem is I aim to take several record of several filed in some SQL table called from another Table. In other words, in userMode containf field ID, level1, level2 etc.. and I just want to take level1 level2.. the ID is used to relation between users and usermode. How shoild it be? thaks.

wysota
13th March 2010, 17:05
I'm afraid I don't understand what you mean. If you want to join two tables then that's not possible with QSqlRelationalTableModel. You can only substitute data from a column with data from some column from another table. To do more complex things you have to use QSqlQueryModel.