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 can I set the id field of an inserted QSqlRecord?
Forum Update on Monday, May 27th 2025

How can I set the id field of an inserted QSqlRecord?

Scheduled Pinned Locked Moved Unsolved General and Desktop
10 Posts 3 Posters 503 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.
  • J Offline
    J Offline
    jdent
    wrote on 1 Apr 2024, 10:11 last edited by jdent 4 Jan 2024, 10:13
    #1

    I have a QSqlRecord with these initializations:

    class PasswordFields
    {
    	QSqlField f1;
    	QSqlField f2;
    	QSqlField f3;
    	QSqlField f4;
    	QSqlRecord rec;
    public:
    	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
    	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
    	{
    		if (id > -1)
    		{
    			f1.setValue(id);
    			rec.append(f1);
    		}
    		f2.setValue(password);
    		f3.setValue(beginDate);
    		f4.setValue(fkLocation);
    		
    		rec.append(f2);
    		rec.append(f3);
    		rec.append(f4);
    	}
    	void setId(int pk)
    	{
    		f1.setValue(pk);
    		rec.append(f1);
    		auto id = rec.value(0).toInt();
    		auto pass = rec.value(1).toString();
    	}
    

    No matter what I do, after I insert the record in the model, there is no way to set the primary key even though I obtain it via

    	auto id = model->query().lastInsertId().toInt();
    

    the pk remains 0 even after calling setId(int pk)!!

    What am I doing wrong?

    J 1 Reply Last reply 1 Apr 2024, 12:03
    1
    • J jdent
      1 Apr 2024, 10:11

      I have a QSqlRecord with these initializations:

      class PasswordFields
      {
      	QSqlField f1;
      	QSqlField f2;
      	QSqlField f3;
      	QSqlField f4;
      	QSqlRecord rec;
      public:
      	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
      	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
      	{
      		if (id > -1)
      		{
      			f1.setValue(id);
      			rec.append(f1);
      		}
      		f2.setValue(password);
      		f3.setValue(beginDate);
      		f4.setValue(fkLocation);
      		
      		rec.append(f2);
      		rec.append(f3);
      		rec.append(f4);
      	}
      	void setId(int pk)
      	{
      		f1.setValue(pk);
      		rec.append(f1);
      		auto id = rec.value(0).toInt();
      		auto pass = rec.value(1).toString();
      	}
      

      No matter what I do, after I insert the record in the model, there is no way to set the primary key even though I obtain it via

      	auto id = model->query().lastInsertId().toInt();
      

      the pk remains 0 even after calling setId(int pk)!!

      What am I doing wrong?

      J Offline
      J Offline
      jdent
      wrote on 1 Apr 2024, 12:03 last edited by
      #2

      @jdent It now works:

      class PasswordFields
      {
      	QSqlField f1;
      	QSqlField f2;
      	QSqlField f3;
      	QSqlField f4;
      	QSqlRecord rec;
      public:
      	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
      	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
      	{
      		if (id > -1)
      		{
      			f1.setValue(id);
      			rec.append(f1);
      		}
      		f2.setValue(password);
      		f3.setValue(beginDate);
      		f4.setValue(fkLocation);
      		
      		rec.append(f2);
      		rec.append(f3);
      		rec.append(f4);
      	}
      	void setId(int pk)
      	{
      		f1.setValue(pk);
      		rec.insert(0, f1);
      	}
      

      The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

      J C 2 Replies Last reply 1 Apr 2024, 12:21
      0
      • J JonB referenced this topic on 1 Apr 2024, 12:20
      • J jdent
        1 Apr 2024, 12:03

        @jdent It now works:

        class PasswordFields
        {
        	QSqlField f1;
        	QSqlField f2;
        	QSqlField f3;
        	QSqlField f4;
        	QSqlRecord rec;
        public:
        	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
        	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
        	{
        		if (id > -1)
        		{
        			f1.setValue(id);
        			rec.append(f1);
        		}
        		f2.setValue(password);
        		f3.setValue(beginDate);
        		f4.setValue(fkLocation);
        		
        		rec.append(f2);
        		rec.append(f3);
        		rec.append(f4);
        	}
        	void setId(int pk)
        	{
        		f1.setValue(pk);
        		rec.insert(0, f1);
        	}
        

        The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

        J Offline
        J Offline
        JonB
        wrote on 1 Apr 2024, 12:21 last edited by JonB 4 Jan 2024, 12:22
        #3

        @jdent
        No cross-reference from the OP, but see also https://forum.qt.io/topic/155730/qsqltablemodel-insertrecord-row-record-always-appends-the-new-record-never-uses-the-row-parameter/9.

        J 1 Reply Last reply 1 Apr 2024, 12:54
        1
        • J JonB
          1 Apr 2024, 12:21

          @jdent
          No cross-reference from the OP, but see also https://forum.qt.io/topic/155730/qsqltablemodel-insertrecord-row-record-always-appends-the-new-record-never-uses-the-row-parameter/9.

          J Offline
          J Offline
          jdent
          wrote on 1 Apr 2024, 12:54 last edited by
          #4

          @JonB This issue has nothing related to my other issue!

          1 Reply Last reply
          0
          • J jdent
            1 Apr 2024, 12:03

            @jdent It now works:

            class PasswordFields
            {
            	QSqlField f1;
            	QSqlField f2;
            	QSqlField f3;
            	QSqlField f4;
            	QSqlRecord rec;
            public:
            	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
            	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
            	{
            		if (id > -1)
            		{
            			f1.setValue(id);
            			rec.append(f1);
            		}
            		f2.setValue(password);
            		f3.setValue(beginDate);
            		f4.setValue(fkLocation);
            		
            		rec.append(f2);
            		rec.append(f3);
            		rec.append(f4);
            	}
            	void setId(int pk)
            	{
            		f1.setValue(pk);
            		rec.insert(0, f1);
            	}
            

            The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

            C Offline
            C Offline
            Christian Ehrlicher
            Lifetime Qt Champion
            wrote on 1 Apr 2024, 13:08 last edited by
            #5

            @jdent said in How can I set the id field of an inserted QSqlRecord?:

            The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

            Because insert does insert it at the specified position and append adds it to the end. Don't know how more obvious to make the function names...

            Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
            Visit the Qt Academy at https://academy.qt.io/catalog

            J 1 Reply Last reply 1 Apr 2024, 15:10
            0
            • C Christian Ehrlicher
              1 Apr 2024, 13:08

              @jdent said in How can I set the id field of an inserted QSqlRecord?:

              The change was in rec.insert(0,f1) instead of rec.append(f1)... why???

              Because insert does insert it at the specified position and append adds it to the end. Don't know how more obvious to make the function names...

              J Offline
              J Offline
              jdent
              wrote on 1 Apr 2024, 15:10 last edited by
              #6

              @Christian-Ehrlicher you misunderstand... of course I understand the difference between insert and append! That's not the point.

              What I don't understand is why programming this method works differently:

              void setId(int pk)
              {
              f1.setValue(pk);
              rec.insert(0, f1); // THIS updates the primary key of the object in memory
              }

              void setId(int pk)
              {
              f1.setValue(pk);
              rec.append(f1); // THIS DOES NOT update the primary key of the object in memory
              }

              Why??

              C 1 Reply Last reply 1 Apr 2024, 15:26
              0
              • J jdent
                1 Apr 2024, 15:10

                @Christian-Ehrlicher you misunderstand... of course I understand the difference between insert and append! That's not the point.

                What I don't understand is why programming this method works differently:

                void setId(int pk)
                {
                f1.setValue(pk);
                rec.insert(0, f1); // THIS updates the primary key of the object in memory
                }

                void setId(int pk)
                {
                f1.setValue(pk);
                rec.append(f1); // THIS DOES NOT update the primary key of the object in memory
                }

                Why??

                C Offline
                C Offline
                Christian Ehrlicher
                Lifetime Qt Champion
                wrote on 1 Apr 2024, 15:26 last edited by
                #7

                @jdent said in How can I set the id field of an inserted QSqlRecord?:

                Why??

                Because, as the function names tell you, f1 is appended to your QSqlRecord (which only contains values).

                Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                Visit the Qt Academy at https://academy.qt.io/catalog

                J 1 Reply Last reply 1 Apr 2024, 15:47
                0
                • C Christian Ehrlicher
                  1 Apr 2024, 15:26

                  @jdent said in How can I set the id field of an inserted QSqlRecord?:

                  Why??

                  Because, as the function names tell you, f1 is appended to your QSqlRecord (which only contains values).

                  J Offline
                  J Offline
                  jdent
                  wrote on 1 Apr 2024, 15:47 last edited by jdent 4 Jan 2024, 15:47
                  #8

                  @Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:

                  class PasswordFields
                  {
                  	QSqlField f1;
                  	QSqlField f2;
                  	QSqlField f3;
                  	QSqlField f4;
                  	QSqlRecord rec;
                  public:
                  	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
                  	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
                  	{
                  		if (id > -1)
                  		{
                  			f1.setValue(id);
                  			rec.append(f1);
                  		}
                  		f2.setValue(password);
                  		f3.setValue(beginDate);
                  		f4.setValue(fkLocation);
                  		
                  		rec.append(f2);
                  		rec.append(f3);
                  		rec.append(f4);
                  	}
                  	void setId(int pk)
                  	{
                  		f1.setValue(pk);
                  		rec.insert(0, f1);
                  	}
                  

                  When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a

                  f1.setValue(id);
                  rec.append(f1);
                  

                  otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call pwd.setId(5);

                  which does nothing to update the f1 field in the record if we use

                  f1.setValue(id);
                  rec.append(f1);
                  

                  On the other hand, if setId() is

                  f1.setValue(id);
                  rec.insert(0,f1);
                  

                  then the f1 field is successfully updated in the record in memory!

                  See the difference? Why?

                  C J 2 Replies Last reply 1 Apr 2024, 16:00
                  0
                  • J jdent
                    1 Apr 2024, 15:47

                    @Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:

                    class PasswordFields
                    {
                    	QSqlField f1;
                    	QSqlField f2;
                    	QSqlField f3;
                    	QSqlField f4;
                    	QSqlRecord rec;
                    public:
                    	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
                    	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
                    	{
                    		if (id > -1)
                    		{
                    			f1.setValue(id);
                    			rec.append(f1);
                    		}
                    		f2.setValue(password);
                    		f3.setValue(beginDate);
                    		f4.setValue(fkLocation);
                    		
                    		rec.append(f2);
                    		rec.append(f3);
                    		rec.append(f4);
                    	}
                    	void setId(int pk)
                    	{
                    		f1.setValue(pk);
                    		rec.insert(0, f1);
                    	}
                    

                    When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a

                    f1.setValue(id);
                    rec.append(f1);
                    

                    otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call pwd.setId(5);

                    which does nothing to update the f1 field in the record if we use

                    f1.setValue(id);
                    rec.append(f1);
                    

                    On the other hand, if setId() is

                    f1.setValue(id);
                    rec.insert(0,f1);
                    

                    then the f1 field is successfully updated in the record in memory!

                    See the difference? Why?

                    C Offline
                    C Offline
                    Christian Ehrlicher
                    Lifetime Qt Champion
                    wrote on 1 Apr 2024, 16:00 last edited by
                    #9

                    Again - your record is ordered. When you access the record with QSqlRecord::value(int) you will get a different value depending on appending or inserting. But you don't show the code you're using the created record. Also I don't understand why you store your data twice - once in separate QSqlFields and once in QSqlRecord.

                    Qt Online Installer direct download: https://download.qt.io/official_releases/online_installers/
                    Visit the Qt Academy at https://academy.qt.io/catalog

                    1 Reply Last reply
                    1
                    • J jdent
                      1 Apr 2024, 15:47

                      @Christian-Ehrlicher Again my point is not being understood. See this code and maybe it can become clear to all finally:

                      class PasswordFields
                      {
                      	QSqlField f1;
                      	QSqlField f2;
                      	QSqlField f3;
                      	QSqlField f4;
                      	QSqlRecord rec;
                      public:
                      	PasswordFields(int id, const QString& password, const QDateTime& beginDate,int fkLocation ) : f1{"id", QMetaType(QMetaType::Int)}, f2{"password", QMetaType(QMetaType::QString)},
                      	f3{"beginDate", QMetaType(QMetaType::QDateTime)},	f4("fkLocation", QMetaType{ QMetaType::Int })
                      	{
                      		if (id > -1)
                      		{
                      			f1.setValue(id);
                      			rec.append(f1);
                      		}
                      		f2.setValue(password);
                      		f3.setValue(beginDate);
                      		f4.setValue(fkLocation);
                      		
                      		rec.append(f2);
                      		rec.append(f3);
                      		rec.append(f4);
                      	}
                      	void setId(int pk)
                      	{
                      		f1.setValue(pk);
                      		rec.insert(0, f1);
                      	}
                      

                      When I call PasswordFields with id > -1, then f1 is set in QSqlRecord by means of a

                      f1.setValue(id);
                      rec.append(f1);
                      

                      otherwise f1 remains unset. When after insertion of the record I finally obtain the QSqlRecord's primary key, I call pwd.setId(5);

                      which does nothing to update the f1 field in the record if we use

                      f1.setValue(id);
                      rec.append(f1);
                      

                      On the other hand, if setId() is

                      f1.setValue(id);
                      rec.insert(0,f1);
                      

                      then the f1 field is successfully updated in the record in memory!

                      See the difference? Why?

                      J Offline
                      J Offline
                      JonB
                      wrote on 1 Apr 2024, 16:35 last edited by
                      #10

                      @jdent
                      Please append a qDebug() statement to each of your append/insert code fragments showing what you are comparing which you say changes/does not change.

                      1 Reply Last reply
                      0

                      10/10

                      1 Apr 2024, 16:35

                      • Login

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