Qt Forum

    • Login
    • Search
    • Categories
    • Recent
    • Tags
    • Popular
    • Users
    • Groups
    • Search
    • Unsolved

    SQL Record ID changes after submitAll

    General and Desktop
    2
    3
    820
    Loading More Posts
    • 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.
    • N
      NitrousUK last edited by

      I'm trying to write some Python code to simply create a record in an SQL database (sqlite backend), and then get the newly created record. Code is called from within an QSqlTableModel.
      The odd thing is that when I do the following, I'm told the generated records id is 0. However after doing a submitAll, it changes. I'm guessing 1 is the starting value and correct, but why am I getting 0 when I use NULL to generate an ID?
      So I'm not sure how I can grab the record I've just inserted, as both the id and row of the record changes after submitAll.

      @
      self.insertRows(0, 1)

          new_record = self.record(0) #Grab newly inserted record
          new_record.setNull("id")  #Generate an id
      
          # new_record.value("id") == 0
          
          self.submitAll()
      
          #Get the record back out
          new_record = self.record(0)
          
          # new_record.value("id") != 0@
      
      1 Reply Last reply Reply Quote 0
      • 8
        8majkel8 last edited by

        maybe you have auto_increment on id in your database ??

        1 Reply Last reply Reply Quote 0
        • N
          NitrousUK last edited by

          Just got "PRIMARY KEY" set on id

          1 Reply Last reply Reply Quote 0
          • First post
            Last post