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. How insert data into QSqlRelationalTableModel that related into another table
Forum Updated to NodeBB v4.3 + New Features

How insert data into QSqlRelationalTableModel that related into another table

Scheduled Pinned Locked Moved Unsolved General and Desktop
6 Posts 3 Posters 1.2k Views
  • 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.
  • M Offline
    M Offline
    mnakhaei
    wrote on last edited by
    #1

    I have QSqlRelationalTableModel that is model of "activity" table

    ActivityHandler::ActivityHandler(QObject *parent) :
        QSqlRelationalTableModel (parent,connection::getInstance ())
    {
        setTable ("activity");
        setRelation (1,QSqlRelation("tags","id","name"));
        select ();
        roles.clear();
        for (int i = 0; i < columnCount(); i++)
        {
            roles[Qt::UserRole + i + 1] = QVariant(this->headerData(i, Qt::Horizontal).toString()).toByteArray();
        }
        setEditStrategy (QSqlRelationalTableModel::OnManualSubmit);
    
    }
    

    and for inserting new data , i use this code ,

    void ActivityHandler::addSample()
    {
        database ().transaction ();
        QSqlRecord r = record ();
        r.setValue ("name","en");
        r.setValue ("duration",30);
        r.setValue ("date","2018-07-18");    
        if(insertRecord(-1, r)){
            submitAll();
            qDebug()<<"successful insertion" << lastError () ;
            database ().commit ();
            qDebug()<<database ().lastError ();
        }
        else
       {
            qDebug()<< lastError ().text ();
            database ().rollback();
        }
    }
    

    its result is

    successful insertion QSqlError("19", "Unable to fetch row", "NOT NULL constraint failed: activity.tagID")
    

    how i can solve this problem ?

    jsulmJ JonBJ 2 Replies Last reply
    0
    • M mnakhaei

      I have QSqlRelationalTableModel that is model of "activity" table

      ActivityHandler::ActivityHandler(QObject *parent) :
          QSqlRelationalTableModel (parent,connection::getInstance ())
      {
          setTable ("activity");
          setRelation (1,QSqlRelation("tags","id","name"));
          select ();
          roles.clear();
          for (int i = 0; i < columnCount(); i++)
          {
              roles[Qt::UserRole + i + 1] = QVariant(this->headerData(i, Qt::Horizontal).toString()).toByteArray();
          }
          setEditStrategy (QSqlRelationalTableModel::OnManualSubmit);
      
      }
      

      and for inserting new data , i use this code ,

      void ActivityHandler::addSample()
      {
          database ().transaction ();
          QSqlRecord r = record ();
          r.setValue ("name","en");
          r.setValue ("duration",30);
          r.setValue ("date","2018-07-18");    
          if(insertRecord(-1, r)){
              submitAll();
              qDebug()<<"successful insertion" << lastError () ;
              database ().commit ();
              qDebug()<<database ().lastError ();
          }
          else
         {
              qDebug()<< lastError ().text ();
              database ().rollback();
          }
      }
      

      its result is

      successful insertion QSqlError("19", "Unable to fetch row", "NOT NULL constraint failed: activity.tagID")
      

      how i can solve this problem ?

      jsulmJ Offline
      jsulmJ Offline
      jsulm
      Lifetime Qt Champion
      wrote on last edited by
      #2

      @mnakhaei You apparently did not set tagID column value. tagID is defined as "NOT NULL", means it has to contain a value.

      https://forum.qt.io/topic/113070/qt-code-of-conduct

      M 1 Reply Last reply
      1
      • M mnakhaei

        I have QSqlRelationalTableModel that is model of "activity" table

        ActivityHandler::ActivityHandler(QObject *parent) :
            QSqlRelationalTableModel (parent,connection::getInstance ())
        {
            setTable ("activity");
            setRelation (1,QSqlRelation("tags","id","name"));
            select ();
            roles.clear();
            for (int i = 0; i < columnCount(); i++)
            {
                roles[Qt::UserRole + i + 1] = QVariant(this->headerData(i, Qt::Horizontal).toString()).toByteArray();
            }
            setEditStrategy (QSqlRelationalTableModel::OnManualSubmit);
        
        }
        

        and for inserting new data , i use this code ,

        void ActivityHandler::addSample()
        {
            database ().transaction ();
            QSqlRecord r = record ();
            r.setValue ("name","en");
            r.setValue ("duration",30);
            r.setValue ("date","2018-07-18");    
            if(insertRecord(-1, r)){
                submitAll();
                qDebug()<<"successful insertion" << lastError () ;
                database ().commit ();
                qDebug()<<database ().lastError ();
            }
            else
           {
                qDebug()<< lastError ().text ();
                database ().rollback();
            }
        }
        

        its result is

        successful insertion QSqlError("19", "Unable to fetch row", "NOT NULL constraint failed: activity.tagID")
        

        how i can solve this problem ?

        JonBJ Offline
        JonBJ Offline
        JonB
        wrote on last edited by
        #3

        @mnakhaei
        You will do yourself & us a favor if your output makes clear just which lastError() is being output when. You show the output all on one line, which implies it all comes from qDebug()<<"successful insertion" << lastError () ;; but I suspect the error part actually comes from qDebug()<<database ().lastError (); after the commit(), and the problem is as @jsulm observes. But without that clarification who knows....

        1 Reply Last reply
        0
        • jsulmJ jsulm

          @mnakhaei You apparently did not set tagID column value. tagID is defined as "NOT NULL", means it has to contain a value.

          M Offline
          M Offline
          mnakhaei
          wrote on last edited by
          #4

          @jsulm activity.tagID have foreign key into tags.name , how can i fill this ? i have tags.name but for give tagID i should query to the database

          jsulmJ JonBJ 2 Replies Last reply
          0
          • M mnakhaei

            @jsulm activity.tagID have foreign key into tags.name , how can i fill this ? i have tags.name but for give tagID i should query to the database

            jsulmJ Offline
            jsulmJ Offline
            jsulm
            Lifetime Qt Champion
            wrote on last edited by
            #5

            @mnakhaei Well, I guess you first need to query for the tag and then use its ID

            https://forum.qt.io/topic/113070/qt-code-of-conduct

            1 Reply Last reply
            0
            • M mnakhaei

              @jsulm activity.tagID have foreign key into tags.name , how can i fill this ? i have tags.name but for give tagID i should query to the database

              JonBJ Offline
              JonBJ Offline
              JonB
              wrote on last edited by JonB
              #6

              @mnakhaei

              1. Presumably activity.tagID ought to be an auto-incrementing number in the database table definition? Or is that tags.id? I get mixed up which way your tables relate.

              2. Insert the row into the table which auto-generates the ID.

              3. Retrieve the auto-generated ID back from the database INSERT.

              4. Set the other table's new row's link column value to that ID.

              5. Insert the row into the other table, containing the link column value.

              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