summaryrefslogtreecommitdiffstats
path: root/src/sql
diff options
context:
space:
mode:
Diffstat (limited to 'src/sql')
-rw-r--r--src/sql/models/qsqltablemodel.cpp15
-rw-r--r--src/sql/models/qsqltablemodel_p.h1
2 files changed, 16 insertions, 0 deletions
diff --git a/src/sql/models/qsqltablemodel.cpp b/src/sql/models/qsqltablemodel.cpp
index a2a83e6a4b..2822c8bb73 100644
--- a/src/sql/models/qsqltablemodel.cpp
+++ b/src/sql/models/qsqltablemodel.cpp
@@ -355,6 +355,16 @@ void QSqlTableModel::setTable(const QString &tableName)
if (d->rec.count() == 0)
d->error = QSqlError(QLatin1String("Unable to find table ") + d->tableName, QString(),
QSqlError::StatementError);
+
+ // Remember the auto index column if there is one now.
+ // The record that will be obtained from the query after select lacks this feature.
+ d->autoColumn.clear();
+ for (int c = 0; c < d->rec.count(); ++c) {
+ if (d->rec.field(c).isAutoValue()) {
+ d->autoColumn = d->rec.fieldName(c);
+ break;
+ }
+ }
}
/*!
@@ -775,6 +785,11 @@ bool QSqlTableModel::submitAll()
}
if (success) {
+ if (d->strategy != OnManualSubmit && mrow.op() == QSqlTableModelPrivate::Insert) {
+ int c = mrow.rec().indexOf(d->autoColumn);
+ if (c != -1 && !mrow.rec().isGenerated(c))
+ mrow.setValue(c, d->editQuery.lastInsertId());
+ }
mrow.setSubmitted();
if (d->strategy != OnManualSubmit)
success = selectRow(row);
diff --git a/src/sql/models/qsqltablemodel_p.h b/src/sql/models/qsqltablemodel_p.h
index 56db09b7e0..825490ea39 100644
--- a/src/sql/models/qsqltablemodel_p.h
+++ b/src/sql/models/qsqltablemodel_p.h
@@ -96,6 +96,7 @@ public:
QSqlIndex primaryIndex;
QString tableName;
QString filter;
+ QString autoColumn;
enum Op { None, Insert, Update, Delete };