PDA

View Full Version : setRelation and NULL in ID



Auryn
18th July 2008, 10:25
PROBLEM:
In a QTableView, if a row has a null value in a combo box field (that writes in the row the value of a foreign key), the user does not see the row (!).

MORE INFORMATION:
I need to have a form where the user enters information about something in a QTableView (for the sake of clarity, I have chosen an example of a database of poems).

Some poems would have author and others not; (the author would be the foreign key). if I try a code like this:



QSqlRelationalTableModel model;
model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.setTable("poems");
model.setRelation(3, QSqlRelation( "authors", "author_id", "name"));
model.setHeaderData(0, Qt::Horizontal, t("ID"));
model.setHeaderData(1, Qt::Horizontal, t("Title"));
model.setHeaderData(2, Qt::Horizontal, t("Year"));
model.setHeaderData(3, Qt::Horizontal, t("Author"));
model.select();

QTableView table_view;
table_view->setModel(model);
table_view->setItemDelegate(new QSqlRelationalDelegate(table_view));


Then the rows that are poems without an author... are not shown.:eek:

jacek
28th July 2008, 12:07
The problem is that Qt uses inner join and you want to use outer join, so instead you can use QSqlTableModel (if you have a view) or QSqlQueryModel. In the latter case the model won't be editable and with view it depends on the underlying DBMS.

bufly
13th August 2008, 09:27
I am also interested in this problem. But I dont understand the answer from jacek. Have someone solved this problem? Thanks.

Auryn
2nd September 2008, 09:38
By now, I will use a record with identifier 0 and empty description in my tables.

See this too:
http://www.qtcentre.org/forum/f-qt-programming-2/t-outer-join-in-qsqlrelationaltablemodel-12784.html

lukeQt
26th May 2015, 16:20
Hi,

I know this is an old thread, but you need to set the setJoinMode. By default the sqlRelationalTableModel uses an inner join. You need to set it to be a left join.
self.setJoinMode(QtSql.QSqlRelationalTableModel.Le ftJoin). This took me forever to figure out because the row would go into the database just not the view.

http://pyqt.sourceforge.net/Docs/PyQt4/qsqlrelationaltablemodel.html#setJoinMode