
Originally Posted by
wysota
What does this print?
for(int i=0;i<record.count();++i){
qDebug() << "Field " << i << "is called" << record.fieldName(i);
}
for(int i=0;i<record.count();++i){
qDebug() << "Field " << i << "is called" << record.fieldName(i);
}
To copy to clipboard, switch view to plain text mode
Oooh, this gives interesting results. It turns out that the field name changes when adding the relation, it's appended with "_2", so "foo_id_2".
Here's some code as an example:
model->setTable("foo");
model
->setRelation
(model
->fieldIndex
("bar_id"),
QSqlRelation("bar",
"id",
"somecolumn"));
model->select();
// the following fails to set the value
row.setValue("bar_id", 123);
// because the following returns -1
qDebug() << row.indexOf("bar_id");
for (int i = 0; i < row.count(); i++) {
qDebug() << "field: " << i << " is called: " << row.fieldName(i);
}
// the above outputs the following:
// field: 0 is called: "blah"
// field: 1 is called: "blah1"
// field: 2 is called: "blah2"
// field: 3 is called: "bar_id_2" <<<< The _2 is appended only when a setRelation() is called on the model.
QSqlRelationalTableModel *model = new QSqlRelationalTableModel(this);
model->setTable("foo");
model->setRelation(model->fieldIndex("bar_id"), QSqlRelation("bar", "id", "somecolumn"));
model->select();
QSqlRecord row = model->record();
// the following fails to set the value
row.setValue("bar_id", 123);
// because the following returns -1
qDebug() << row.indexOf("bar_id");
for (int i = 0; i < row.count(); i++) {
qDebug() << "field: " << i << " is called: " << row.fieldName(i);
}
// the above outputs the following:
// field: 0 is called: "blah"
// field: 1 is called: "blah1"
// field: 2 is called: "blah2"
// field: 3 is called: "bar_id_2" <<<< The _2 is appended only when a setRelation() is called on the model.
To copy to clipboard, switch view to plain text mode
So, doesn't this happen if you try my example code? Is this standard / normal behaviour?
Bookmarks