Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

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 :(



  • @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