Qt: viewing data from tables with foreign key columns pointing to multiple other tables
I have a table Thingsies
, with columns id
, data1
, otherTableId
, otherTableRowId
. I need the last 开发者_JAVA百科two columns instead of just a foreign key column because depending on the Thingy
the type of information I want there is different.
otherTableId
references an id
in the table OtherTables
, which itself has columns called id
and tableName
. These tableNames
are the names of other tables, like OtherTable1
, OtherTable2
.
The column otherTableRowId
in Thingies
references an id
in the OtherTable
given by otherTableId
. The OtherTables
themselves have more data in them. There's a one-to-one correspondence between Thingies and entries in one of the OtherTables
. So the number of rows in Thingies is the total number of rows in all the OtherTables
. (Is this bad design?)
I want to display Thingies in a QTableView
that has columns for id
, data1
, and data2
, where data2
is generated programatically from otherTableId
and otherTableRowId
. How should I proceed?
Thanks.
I think I've solved this. I'm using a QSqlQueryModel
whose query is a union of a bunch of select statements, one for each OtherTable
. Each select statement has to somehow merge (e.g., concatenate) the data unique to each OtherTable
using a special expression anyway, so there's no use for me at least for the OtherTables
table.
精彩评论