I have encountered a problem using QSqlTableModel on PostgreSQL view. Let me show the relevance code here first.
a view is created with the following code running on PostgreSQL 9.2.4:
CREATE OR REPLACE VIEW reporters_spaces_edit AS
SELECT reporters.name AS reporter_name, spaces.name AS space_name,
reporters_spaces.reporter_id IS NOT NULL AND reporters_spaces.space_id IS NOT NULL AS linked
FROM reporters
CROSS JOIN spaces
LEFT JOIN reporters_spaces ON reporters.id = reporters_spaces.reporter_id AND reporters_spaces.space_id = spaces.id;
My C++ code compiled on VC2005 with Qt 4.4.0 looks like this:
void populateModel()
{
QTableView lrView;
QSqlDatabase lrDb(QSqlDatabase::addDatabase("QPSQL"));
...
QSqlTableModel lrModel(lrDb);
lrModel.setTable("reporters_spaces_edit");
if (!lrModel.select())
{
QMessageBox::critical(
0,
tr("Database Error"),
lrModel.lastError().text());
return;
}
lrView.setModel(lrModle);
}
It shows "Unable to find table reporters_spaces_edit" on the message box.
I tried to find out all the views in the database. I added the following line:
QPlainTextEdit lrEdit;
lrEdit->setPlainText(lrDb.table(QSql::Views).join("\n"));
The resulting list is the same as
QPlainTextEdit lrEdit;
lrEdit->setPlainText(lrDb.table(QSql::Tables).join("\n"));
Does it mean SQL view is not supported on QSqlTableModel when running on PostgreSQL?
Many Thanks!