QSqlTableModel UNIQUE constraint



  • I have the following table in SQLite3 database:

    CREATE TABLE m_units
    (
        id INTEGER PRIMARY KEY NOT NULL,
        m_unit TEXT UNIQUE NOT NULL
    )
    

    It is mapped to QSqlTableModel. I use the following code to insert new records:

    void ModelsContainer::addRegistryRecord(RegistryType type, const QSqlRecord &record)
    {
        if(tableModels->value((*registryMap)[type])->insertRecord(-1, record)) {
            tableModels->value((*registryMap)[type])->submitAll();
        }
        else
        {
            QMessageBox::critical(nullptr,"Error!","Violation of the UNIQUE constraint in the table!");
        }
    }
    

    I supposed that insertRecord() must return false if record's contents conflict with the table's constrains, but it isn't like I thought.
    app_screenshot_unique_constraint_violation
    What approach should I use on tablemodels' editing to check constraint's violations in the underlying db tables?



  • I don't believe that I need to check for per-column duplicates by hand :(


  • Moderators

    @Tikani

    QSql classes are basically interface classes working with external drivers.

    The UNIQUE contraint is part of the SQLite implementation and not the QSql implementation.


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.