PDA

View Full Version : QSqlRelationalTableModel produces useless Sql statement



ObiWanKenobe
25th October 2011, 21:14
Hi everybody,

I have a problem with the QSqlRelationalTableModel.
I have the following code:



patientModel = new QSqlRelationalTableModel(this, myDb);
patientModel->setTable("PATIENTEN");
patientModel->setRelation(patientModel->fieldIndex("ANGELEGTVON"), QSqlRelation("BENUTZER", "PK_ID", "KRZ")); /*created by*/
patientModel->setRelation(patientModel->fieldIndex("GEAENDERTVON"), QSqlRelation("BENUTZER", "PK_ID", "KRZ"));/*changed by*/
patientModel->setRelation(patientModel->fieldIndex("BEHANDLER"), QSqlRelation("BENUTZER", "PK_ID", "KRZ"));/*doctor*/
patientModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
if (PatId > 0)
patientModel->setFilter(QString ("PATIENTEN.PK_ID = %1").arg(m_PatId));
patientModel->select();


When I call patientModel->select()then, QSqlRelationalTableModel produces the following Sql statement:


SELECT
PATIENTEN."PK_ID", PATIENTEN."VORNAME", PATIENTEN."NACHNAME", ...
relTblAl_10.KRZ AS BENUTZER_KRZ_3,
relTblAl_12.KRZ AS BENUTZER_KRZ_2,
relTblAl_16.KRZ, ...
FROM
PATIENTEN, BENUTZER relTblAl_10, BENUTZER relTblAl_12, BENUTZER relTblAl_16
WHERE
(PATIENTEN."BEHANDLER" = relTblAl_10.PK_ID AND
PATIENTEN."GEAENDERTVON" = relTblAl_12.PK_ID AND
PATIENTEN."ANGELEGTVON" = relTblAl_16.PK_ID) AND
(PATIENTEN.PK_ID = 2678)

It seems correct, but it doesn't work most of the time, because "GEAENDERTVON" (e.g. "changed by") is most of the time NULL and the result set is empty.
The correct way would be a LEFT OUTER JOIN.

Is this a bug or am I missing something?

Thanks in advance.

Lesiok
26th October 2011, 09:01
This is not a bug and You don't missing something. QSqlRelationalTableModel is as is.

ObiWanKenobe
26th October 2011, 12:59
This is not a bug and You don't missing something. QSqlRelationalTableModel is as is.
Thanks for your reply, Lesiok.

If you ask me, it's a bug. Or at least there's a lack of implementation.
What is QSqlRelationalTableModel good for, if it can not handle NULL foreign keys.

Wouldn' you agree?

Lesiok
26th October 2011, 15:12
This is a lack of implementation. So we have our SqlRelationalModel.