Skip to content

Commit

Permalink
postgres provider: drop implicit not null and unique constraint for c…
Browse files Browse the repository at this point in the history
…olumns in compound keys (fixes #20073)

(cherry picked from commit f8e32f7)
  • Loading branch information
jef-n committed Apr 17, 2019
1 parent 2ce3887 commit 30e808e
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 14 deletions.
10 changes: 5 additions & 5 deletions src/providers/postgres/qgspostgresprovider.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1048,9 +1048,9 @@ bool QgsPostgresProvider::loadFields()
QgsField newField = QgsField( fieldName, fieldType, fieldTypeName, fieldSize, fieldPrec, fieldComment, fieldSubType );

QgsFieldConstraints constraints;
if ( notNullMap[tableoid][attnum] || mPrimaryKeyAttrs.contains( i ) )
if ( notNullMap[tableoid][attnum] || ( mPrimaryKeyAttrs.size() == 1 && mPrimaryKeyAttrs[0] == i ) || identityMap[tableoid][attnum] != ' ' )
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
if ( uniqueMap[tableoid][attnum] || mPrimaryKeyAttrs.contains( i ) )
if ( uniqueMap[tableoid][attnum] || ( mPrimaryKeyAttrs.size() == 1 && mPrimaryKeyAttrs[0] == i ) || identityMap[tableoid][attnum] != ' ' )
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
newField.setConstraints( constraints );

Expand Down Expand Up @@ -1424,13 +1424,13 @@ bool QgsPostgresProvider::determinePrimaryKey()
determinePrimaryKeyFromUriKeyColumn();
}

Q_FOREACH ( int fieldIdx, mPrimaryKeyAttrs )
if ( mPrimaryKeyAttrs.size() == 1 )
{
//primary keys are unique, not null
QgsFieldConstraints constraints = mAttributeFields.at( fieldIdx ).constraints();
QgsFieldConstraints constraints = mAttributeFields.at( mPrimaryKeyAttrs[0] ).constraints();
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
mAttributeFields[ fieldIdx ].setConstraints( constraints );
mAttributeFields[ mPrimaryKeyAttrs[0] ].setConstraints( constraints );
}

mValid = mPrimaryKeyType != PktUnknown;
Expand Down
2 changes: 1 addition & 1 deletion tests/src/python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,6 @@ ADD_PYTHON_TEST(PyQgsPanelWidgetStack test_qgspanelwidgetstack.py)
ADD_PYTHON_TEST(PyQgsPoint test_qgspoint.py)
ADD_PYTHON_TEST(PyQgsPointClusterRenderer test_qgspointclusterrenderer.py)
ADD_PYTHON_TEST(PyQgsPointDisplacementRenderer test_qgspointdisplacementrenderer.py)
ADD_PYTHON_TEST(PyQgsPostgresDomain test_qgspostgresdomain.py)
ADD_PYTHON_TEST(PyQgsProcessingRecentAlgorithmLog test_qgsprocessingrecentalgorithmslog.py)
ADD_PYTHON_TEST(PyQgsProcessingInPlace test_qgsprocessinginplace.py)
ADD_PYTHON_TEST(PyQgsProcessingAlgRunner test_qgsprocessingalgrunner.py)
Expand Down Expand Up @@ -256,6 +255,7 @@ ENDIF (WITH_DESKTOP)

IF (ENABLE_PGTEST)
ADD_PYTHON_TEST(PyQgsPostgresProvider test_provider_postgres.py)
ADD_PYTHON_TEST(PyQgsPostgresDomain test_qgspostgresdomain.py)
ADD_PYTHON_TEST(PyQgsPostgresTransaction test_qgspostgrestransaction.py)
ADD_PYTHON_TEST(PyQgsRelationEditWidget test_qgsrelationeditwidget.py)
ADD_PYTHON_TEST(PyQgsVectorLayerTools test_qgsvectorlayertools.py)
Expand Down
12 changes: 9 additions & 3 deletions tests/src/python/test_provider_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -441,7 +441,7 @@ def testTransactionDirty(self):
ft1 = vl.getFeatures('pk=1')
self.assertFalse(ft1.nextFeature(f))

def testTransactionConstrains(self):
def testTransactionConstraints(self):
# create a vector layer based on postgres
vl = QgsVectorLayer(self.dbconn + ' sslmode=disable key=\'id\' table="qgis_test"."check_constraints" sql=', 'test', 'postgres')
self.assertTrue(vl.isValid())
Expand Down Expand Up @@ -626,7 +626,7 @@ def testJson(self):
fi = vl.getFeatures(QgsFeatureRequest())
f = QgsFeature()

#test list
# test list
fi.nextFeature(f)
value_idx = vl.fields().lookupField('jvalue')
self.assertIsInstance(f.attributes()[value_idx], list)
Expand All @@ -638,7 +638,7 @@ def testJson(self):
self.assertEqual(f.attributes()[value_idx], [4, 5, 6])
self.assertEqual(f.attributes()[value_idx], [4.0, 5.0, 6.0])

#test dict
# test dict
fi.nextFeature(f)
value_idx = vl.fields().lookupField('jvalue')
self.assertIsInstance(f.attributes()[value_idx], dict)
Expand Down Expand Up @@ -1248,6 +1248,12 @@ def uncompiledFilters(self):
def partiallyCompiledFilters(self):
return set([])

def testConstraints(self):
for key in ["key1", "key2"]:
idx = self.vl.dataProvider().fieldNameIndex(key)
self.assertTrue(idx >= 0)
self.assertFalse(self.vl.dataProvider().fieldConstraints(idx) & QgsFieldConstraints.ConstraintUnique)


if __name__ == '__main__':
unittest.main()
8 changes: 4 additions & 4 deletions tests/testdata/provider/testdata_pg.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

set -e

echo "SET client_min_messages TO WARNING;" >> ~/.psqlrc
DB=${DB:-qgis_test}

SCRIPTS="
tests/testdata/provider/testdata_pg.sql
Expand All @@ -15,8 +15,8 @@ SCRIPTS="
tests/testdata/provider/testdata_pg_json.sql
"

dropdb qgis_test 2> /dev/null || true
createdb qgis_test -E UTF8 -T template0 || exit 1
dropdb --if-exists $DB
createdb $DB -E UTF8 -T template0
for f in ${SCRIPTS}; do
psql -q --echo-errors -f $f qgis_test -v ON_ERROR_STOP=1
psql -q --echo-errors -c "SET client_min_messages TO WARNING;" -f $f $DB -v ON_ERROR_STOP=1
done
6 changes: 5 additions & 1 deletion tests/testdata/provider/testdata_pg.sql
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,9 @@ CREATE EXTENSION IF NOT EXISTS postgis;

DROP SCHEMA IF EXISTS qgis_test CASCADE;
CREATE SCHEMA qgis_test;

GRANT ALL ON SCHEMA qgis_test TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA qgis_test GRANT ALL ON TABLES TO public;
ALTER DEFAULT PRIVILEGES IN SCHEMA qgis_test GRANT ALL ON SEQUENCES TO public;

SET default_tablespace = '';

Expand Down Expand Up @@ -467,6 +469,8 @@ INSERT INTO qgis_test.rename_table (field1,field2) VALUES ('a','b');
-- Table for editor widget types
--

DROP TABLE IF EXISTS qgis_editor_widget_styles;

CREATE TABLE qgis_editor_widget_styles
(
schema_name TEXT NOT NULL,
Expand Down

0 comments on commit 30e808e

Please sign in to comment.