PostgreSql and problem with lastInsertId()
-
I have a problem with method lastInsertId. I'm using a PostgreSql 8.4 and Qt 4.6.3 under Linux. I have a code like this:
@
// _model is a QSqlTableModel
QSqlRecord record = _model->record();
_model->insertRecord( -1, record );SomeDialog* dialog = new SomeDialog( _model ); dialog->exec(); if( dialog->result() == QDialog::Accepted ) { bool ok = _model->submitAll(); if( ok ) { // allways 0 but in db is a new record qDebug() << _model->query().lastInsertId().toInt(); } else { qDebug() << _model->lastError().text(); } }
@
My table looks like this:
@
CREATE TABLE mytable
(
id serial NOT NULL,
field1 text,
CONSTRAINT mytable_pkey PRIMARY KEY (id),
)
WITH (
OIDS=TRUE
);
@How can I get a inserted ID? I need select new row in QTableView and I need this information.
-
As far as I know lastInsertId() for PostgreSQL is supported at least with Qt 4.7. If this is not an option for you you might try the "RETURNING":http://www.postgresql.org/docs/8.3/interactive/sql-insert.html clause in your queries.
-
Thanks for reply. Tomorrow I will test Qt 4.7. lastInsertId() returns a OIDS number from Pg(I know from Qt doc), but only in QSqlQuery(I tested this a few minuts ago), but with QSqlTableModel return always 0. I known a RETURNING but I want to stay with QSqlTableModel. Maybe I will change a little driver to Pg to implement a insert with returning. I need think about it what would be better.