Reputation: 51
I am working with a QSqlRelationalTableModel, and have for the most part foreign key ids resolving to their name values via QsqlRelation quite happily:
QSqlRelationalTableModel *model = db->get_db_model();
model->setEditStrategy(QSqlTableModel::OnFieldChange);
model->setTable("materialproperty");
model->select();
model->setRelation(1, QSqlRelation("parameter", "id", "name"));
MyTableview->setModel(model);
works fine. But one particular relation:
model->setRelation(2, QSqlRelation("other", "id", "name"));
is causing the whole model to fail. The only difference between these two situations is that column 1 in table materialproperty is always populated, whereas 2 is often NULL. Is there a workaround for this behaviour, or another explanation? My ID fields are always text uuids. Btw If I construct a QSqlRelation and test it's validity, it passes ok.
Upvotes: 1
Views: 1052
Reputation: 11
It works if you set the Left Join like this:
m_pModel->setJoinMode(QSqlRelationalTableModel::LeftJoin);
Upvotes: 1
Reputation: 141
By default QString QSqlTableModel::selectStatement() returns something like that:
SELECT ...
FROM table1 t1, table2 t2
WHERE (t1.fk = t2.id) AND (<filter stuff>)
ORDER BY <something>
Try to reimplement it to return something like that:
SELECT ...
FROM table1 t1 LEFT JOIN table2 t2 ON t1.fk = t2.id
WHERE <filter stuff>
ORDER BY <something>
Upvotes: 1