Results 1 to 3 of 3

Thread: how to clear relationship

  1. #1
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default how to clear relationship

    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:

    Qt Code:
    1. ...
    2. rtblmdl = new QSqlRelationalTableModel(this,ggdb);
    3. ...
    4. rtblmdl->setTable("driver");
    5. rtblmdl->setRelation(2,QSqlRelation("cars","id","car"));
    6. rtblmdl->setJoinMode(QSqlRelationalTableModel::LeftJoin);
    7. rtblmdl->setEditStrategy(QSqlTableModel::OnManualSubmit);
    8.  
    9. ...
    10. // join to QTableView ( tblW )
    11. ui->tblW->setModel(mrtblmdl);
    12. ui->tblW->setItemDelegate(new QSqlRelationalDelegate(ui->tblW));
    13. ui->tblW->hideColumn(0);
    14. ...
    15.  
    16.  
    17.  
    18. ...
    19. // set record "QSqlRecord" ( rec is private variable ) after editting
    20. rec.setValue(i,lnEdt[i]->text());
    21. ...
    22. // write in to table
    23. rtblmdl->setRecord(ui->tblW->currentIndex().row(),rec);
    24. rtblmdl->submitAll();
    25. ...
    26. // write into table another method
    27. rtblmdl->setData(rtblmdl->index(ui->tblW->currentIndex().row(),5),QVariant::fromValue(1));
    28. rtblmdl->submitAll();
    To copy to clipboard, switch view to plain text mode 



    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.
    Last edited by milos1; 27th July 2015 at 13:00.

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: how to clear relationship

    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...

  3. #3
    Join Date
    Jul 2015
    Posts
    3
    Qt products
    Qt5
    Platforms
    Windows

    Default Re: how to clear relationship

    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.
    Last edited by milos1; 28th July 2015 at 07:16.

Similar Threads

  1. Facebook Relationship Visualization
    By Sven in forum Qt-based Software
    Replies: 3
    Last Post: 10th November 2014, 14:26
  2. what's the relationship between QOCI and OCI ?
    By silentyears in forum Qt Programming
    Replies: 6
    Last Post: 14th March 2014, 14:29
  3. Replies: 1
    Last Post: 18th April 2012, 17:35
  4. How to get the relationship between two QDockWidget?
    By yunxiaodong in forum Qt Programming
    Replies: 0
    Last Post: 16th August 2011, 09:32
  5. Relationship b/w different classes
    By salmanmanekia in forum Newbie
    Replies: 0
    Last Post: 2nd June 2010, 19:24

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.