Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. QSqlTableModel UNIQUE constraint
Forum Updated to NodeBB v4.3 + New Features

QSqlTableModel UNIQUE constraint

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 1.5k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    Tikani
    wrote on last edited by
    #1

    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?

    1 Reply Last reply
    0
    • T Offline
      T Offline
      Tikani
      wrote on last edited by
      #2

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

      K 1 Reply Last reply
      0
      • T Tikani

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

        K Offline
        K Offline
        koahnig
        wrote on last edited by
        #3

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

        Vote the answer(s) that helped you to solve your issue(s)

        1 Reply Last reply
        1

        • Login

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • Users
        • Groups
        • Search
        • Get Qt Extensions
        • Unsolved