Qt Forum

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

    Unsolved How to determine if primary key is generated always or by default

    General and Desktop
    2
    3
    486
    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.
    • dadler
      dadler last edited by dadler

      Using DB2, we have tables which use the following definitions to create primary keys:
      CREATE TABLE TEST.ZIPPOINT(
      objectid BIGINT NOT NULL PRIMARY KEY GENERATED ALWAYS AS IDENTITY
      and
      CREATE TABLE TEST.ZIPPOLY(
      objectid INTEGER NOT NULL PRIMARY KEY GENERATED BY DEFAULT AS IDENTITY

      We use code like the following to get the primary key information:
      QSqlIndex pk = mDatabase.primaryIndex( table );
      QSqlField pkFld = pk.field( 0 );
      QgsDebugMsg(QString("auto: %1; generated: %2" ).arg(pkFld.isAutoValue()).arg(pkFld.isGenerated()));

      But isAutoValue() is always 0 and isGenerated() is always 1

      The documentation states:
      *bool QSqlField::isAutoValue() const

      Returns true if the value is auto-generated by the database, for example auto-increment primary key values.

      bool QSqlField::isGenerated() const

      Returns true if the field is generated; otherwise returns false.*

      It isn't clear what "generated" actually means for the field.

      dadler 1 Reply Last reply Reply Quote 0
      • dadler
        dadler @dadler last edited by

        @dadler From looking at the source, it appears that the ODBC driver that we are using does not call setAutoValue so isAuto will always be false. This is also true of the DB2-specific driver.

        isGenerated has to do with whether or not SQL is generated for a field and doesn't have anything to do with primary key generation.

        1 Reply Last reply Reply Quote 0
        • mrjj
          mrjj Lifetime Qt Champion last edited by

          @dadler said:
          Hi
          The doc says this for ODBC
          http://doc.qt.io/qt-5.5/qsqlfield.html#isAutoValue

          But it seems u found that its also true for the DB2 driver?

          Thank you for updating the post.

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