Skip to content
Permalink
Browse files

postgres provider: drop implicit not null and unique constraint for c…

…olumns in compound keys (fixes #20073)

(cherry picked from commit 30e808e & 267599b)
  • Loading branch information
jef-n committed Apr 19, 2019
1 parent d16bf3f commit 72a48478708c1ca6816a4cf89fb4b890055589dd
@@ -1055,9 +1055,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 ) )
constraints.setConstraint( QgsFieldConstraints::ConstraintNotNull, QgsFieldConstraints::ConstraintOriginProvider );
if ( uniqueMap[tableoid][attnum] || mPrimaryKeyAttrs.contains( i ) )
if ( uniqueMap[tableoid][attnum] || ( mPrimaryKeyAttrs.size() == 1 && mPrimaryKeyAttrs[0] == i ) )
constraints.setConstraint( QgsFieldConstraints::ConstraintUnique, QgsFieldConstraints::ConstraintOriginProvider );
newField.setConstraints( constraints );

@@ -1431,13 +1431,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;
@@ -143,7 +143,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)
@@ -244,6 +243,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)
@@ -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())
@@ -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)
@@ -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)
@@ -1214,6 +1214,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()
@@ -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
@@ -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
@@ -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 = '';

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

0 comments on commit 72a4847

Please sign in to comment.
You can’t perform that action at this time.