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 to determine if primary key is generated always or by default

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

Scheduled Pinned Locked Moved Unsolved General and Desktop
3 Posts 2 Posters 689 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.
  • dadlerD Offline
    dadlerD Offline
    dadler
    wrote on last edited by dadler
    #1

    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.

    dadlerD 1 Reply Last reply
    0
    • dadlerD 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.

      dadlerD Offline
      dadlerD Offline
      dadler
      wrote on last edited by
      #2

      @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
      0
      • mrjjM Offline
        mrjjM Offline
        mrjj
        Lifetime Qt Champion
        wrote on last edited by
        #3

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

        • Login

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