Skip to content
Permalink
Browse files

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

…olumns in compound keys (fixes #20073)
  • Loading branch information
jef-n committed Apr 7, 2019
1 parent 59dd851 commit 2826ba34f9c2a65a419e908d981a7c90447417bc
Showing with 5 additions and 5 deletions.
  1. +5 −5 src/providers/postgres/qgspostgresprovider.cpp
@@ -1054,9 +1054,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 );

@@ -1453,13 +1453,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;

0 comments on commit 2826ba3

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