Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bugfix gh50168 pg identity default #50230

Merged
merged 2 commits into from
Sep 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1418,7 +1418,7 @@ bool QgsPostgresProvider::loadFields()
&& defValMap[tableoid][attnum].isEmpty() )
{
const QString seqName { mTableName + '_' + fieldName + QStringLiteral( "_seq" ) };
const QString seqSql = QStringLiteral( "SELECT c.oid "
const QString seqSql = QStringLiteral( "SELECT c.oid::regclass "
" FROM pg_class c "
" LEFT JOIN pg_namespace n "
" ON ( n.oid = c.relnamespace ) "
Expand All @@ -1430,7 +1430,7 @@ bool QgsPostgresProvider::loadFields()
QgsPostgresResult seqResult( connectionRO()->PQexec( seqSql ) );
if ( seqResult.PQntuples() == 1 )
{
defValMap[tableoid][attnum] = QStringLiteral( "nextval(%1::regclass)" ).arg( quotedValue( seqName ) );
defValMap[tableoid][attnum] = QStringLiteral( "nextval(%1::regclass)" ).arg( quotedValue( seqResult.PQgetvalue( 0, 0 ) ) );
}
}

Expand Down
20 changes: 19 additions & 1 deletion tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -2567,7 +2567,7 @@ def testIdentityPk(self):
feature.setGeometry(geom)
self.assertTrue(vl.dataProvider().addFeature(feature))

self.assertEqual(vl.dataProvider().defaultValueClause(0), "nextval('b29560_gid_seq'::regclass)")
self.assertEqual(vl.dataProvider().defaultValueClause(0), "nextval('qgis_test.b29560_gid_seq'::regclass)")

del (vl)

Expand All @@ -2580,6 +2580,24 @@ def testIdentityPk(self):
feature = next(vl.getFeatures())
self.assertIsNotNone(feature.id())

def testEvalDefaultOnProviderSide(self):
"""Test issue GH #50168"""

self.execSQLCommand(
'DROP TABLE IF EXISTS qgis_test."gh_50168" CASCADE')
self.execSQLCommand(
'CREATE TABLE qgis_test."gh_50168" ( id integer generated always as identity primary key, test_string VARCHAR(128) )')

vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'id\' srid=4326 table="qgis_test"."gh_50168" () sql=', 'gh_50168', 'postgres')
self.assertTrue(vl.isValid())
vl.dataProvider().setProviderProperty(QgsVectorDataProvider.EvaluateDefaultValues, True)

f = QgsFeature(vl.fields())
f.setAttribute('test_string', 'QGIS Rocks')

dp = vl.dataProvider()
self.assertNotEqual(dp.defaultValue(0), QVariant())

@unittest.skipIf(os.environ.get('QGIS_CONTINUOUS_INTEGRATION_RUN', 'true'), 'Test flaky')
def testDefaultValuesAndClauses(self):
"""Test whether default values like CURRENT_TIMESTAMP or
Expand Down