PDA

View Full Version : how to clear relationship



milos1
27th July 2015, 12:54
Hi !

I created a class for editing database tables (SQLITE) with the relationship ( LEFT JOIN ) to another table with enable NULL for FK.
Example:
master table:
driver:
ID, NAME, CAR
subtable:
cars:
ID, TYPE, COLOR
Here is a snippet of code:


...
rtblmdl = new QSqlRelationalTableModel(this,ggdb);
...
rtblmdl->setTable("driver");
rtblmdl->setRelation(2,QSqlRelation("cars","id","car"));
rtblmdl->setJoinMode(QSqlRelationalTableModel::LeftJoin);
rtblmdl->setEditStrategy(QSqlTableModel::OnManualSubmit);

...
// join to QTableView ( tblW )
ui->tblW->setModel(mrtblmdl);
ui->tblW->setItemDelegate(new QSqlRelationalDelegate(ui->tblW));
ui->tblW->hideColumn(0);
...



...
// set record "QSqlRecord" ( rec is private variable ) after editting
rec.setValue(i,lnEdt[i]->text());
...
// write in to table
rtblmdl->setRecord(ui->tblW->currentIndex().row(),rec);
rtblmdl->submitAll();
...
// write into table another method
rtblmdl->setData(rtblmdl->index(ui->tblW->currentIndex().row(),5),QVariant::fromValue(1));
rtblmdl->submitAll();





The cells of the master table ( "driver" ) I do not editing by qtableview, but separate EditBoxs (one EditBox for each column ).
However, if I need erase cell with FK (cancel a relationship), i setting the appropriate item in QSqlRecord to NULL, this value after call methods QSqlRelationalTableModel.SubmitAll not write ( not delete relationship )
I also tried writing by QSqlRelationalTableModel.setData unfortunately with the same result.

Thanks fro any response.

ChrisW67
27th July 2015, 22:57
If the ID column you are trying to set to null is declared as the primary key then it cannot be null (although Sqlite may allow this for legacy compatibility). If it is "integer primary key" then it absolutely cannot be null.

Look at the lastError() value on the database object for more clues.
Post your actual table definitions...

milos1
28th July 2015, 06:43
Thanks ChrisW67 for reply.

I'm sorry for my English, maybe that is why we did not understand.
I do not need to delete primary key, but foreign keys ( FK in table driver, as the driver can be free of car ).
Definitions of table "DRIVER" : CREATE TABLE "driver" ("ID" INTEGER PRIMARY KEY NOT NULL , "NAME" TEXT, "CAR" INTEGER)
where column "CAR" contain foreign key from table "CARS".
LastError() does not contain any errors.
I have to allow the driver to remove car, so delete the primary key, so delete relationship.

If create a new entry in table driver, does not fill a column CAR, write by
" rtblmdl->setRecord(ui->tblW->currentIndex().row(),rec);
rtblmdl->submitAll();"

ended without error, and column CAR containing NULL.

If create a new entry in table driver, with fill a column CAR (FK from table CARS), without error. But if i need delete this FK, write by
" rtblmdl->setRecord(ui->tblW->currentIndex().row(),rec);
rtblmdl->submitAll();"
ended without error, but FK don't delete.

Milos.