I have a problem with inserting new QSqlRecord to Postgresql database. Target table is defined as follows:
create table samochod(
samochod_id integer primary key default nextval('samochod_id_seq'),
marka varchar(30),
poj_silnika numeric,
typ_silnika silnik_typ,
liczba_osob smallint
);
And code of record insertion is:
QSqlRecord rec = model->record();
rec.setGenerated("samochod_id", false);
rec.setValue("marka", ui.brandEdit->text());
rec.setValue("poj_silnika", ui.volSpin->value());
rec.setValue("typ_silnika", ui.engineCombo->currentText());
rec.setValue("liczba_osob", ui.passengersSpin->value());
bool ok = model->insertRecord(-1,rec);
All I want to do is insert a new record, and make sql set its samochod_id for me, but despite I set isGenerated to false it crashes with message:
"ERROR: null value in column "samochod_id" violates not-null constraint
QPSQL: Unable to create query"
What should I do to make QSqlRecord leave generation of samochod_id to database?
You can use removeColumn(int) before QSqlRecord rec = model->record() and it should work. If model is associated to a view (a QTableView instance, for example), you'll be effectively hiding the samochod_id column, though.
Try this:
// get the index of the id column
int colId = model.fieldIndex("samochod_id");
// remove the column from the model
model.removeColumn(colId);
QSqlRecord rec = model->record();
// rec.setGenerated("samochod_id", false); /// not needed anymore
rec.setValue("marka", ui.brandEdit->text());
rec.setValue("poj_silnika", ui.volSpin->value());
rec.setValue("typ_silnika", ui.engineCombo->currentText());
rec.setValue("liczba_osob", ui.passengersSpin->value());
bool ok = model->insertRecord(-1,rec);
I can understand why you prefer to insert records this way (avoid writing SQL manually).
I think using a QSqlTableModel instance just for the purpose of inserting records is too expensive (models are complex beasts), so I prefer to use a plain QSqlQuery. With this approach it is not necessary to instantiate multiple QSqlTableModels if you need to insert records in multiple tables in a transaction, a single QSqlQuery is enough:
QSqlDatabase::database("defaultdb").transaction();
QSqlQuery query(QSqlDatabase::database("defaultdb"));
query.prepare(QString("INSERT INTO table1 (field1, field2) VALUES(?,?)"));
query.bindValue(0,aString);
query.bindValue(1,anotherString);
query.exec();
// insert in another table
query.exec(QString("INSERT INTO table2 (field1, field2) VALUES(...)");
bool ok = QSqlDatabase::database("defaultdb").commit();
query.finish();
if(!ok){
// do something
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With