How to determine if primary key is generated always or by default
-
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 IDENTITYWe 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() constReturns 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.
-
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 IDENTITYWe 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() constReturns 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 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.
-
@dadler said:
Hi
The doc says this for ODBC
http://doc.qt.io/qt-5.5/qsqlfield.html#isAutoValueBut it seems u found that its also true for the DB2 driver?
Thank you for updating the post.