Skip to content

Commit

Permalink
Fix default values for linking features in N:M
Browse files Browse the repository at this point in the history
Followup bfa507a

Default values for attributes on the linking table have not been
respected and therefore it was impossible to create entries on the
linking table with the combination of NOT NULL + default value.
  • Loading branch information
m-kuhn committed May 19, 2017
1 parent cc6f6e8 commit 99a2f3b
Showing 1 changed file with 12 additions and 3 deletions.
15 changes: 12 additions & 3 deletions src/gui/qgsrelationeditorwidget.cpp
Expand Up @@ -361,10 +361,14 @@ void QgsRelationEditorWidget::linkFeature()
.setFilterFids( selectionDlg.selectedFeatures() )
.setSubsetOfAttributes( mNmRelation.referencedFields() ) );

QgsFields fields = mRelation.referencingLayer()->fields();
int attrCount = fields.size();
int firstIdx = fields.indexFromName( mRelation.fieldPairs().at( 0 ).first );
int secondIdx = mNmRelation.referencingFields().at( 0 );
QgsFeature relatedFeature;

QgsFeatureList newFeatures;
QgsFeature linkFeature( mRelation.referencingLayer()->fields() );
QgsFeature linkFeature( fields );

Q_FOREACH ( const QgsRelation::FieldPair& fieldPair, mRelation.fieldPairs() )
{
Expand All @@ -373,9 +377,14 @@ void QgsRelationEditorWidget::linkFeature()

while ( it.nextFeature( relatedFeature ) )
{
Q_FOREACH ( const QgsRelation::FieldPair& fieldPair, mNmRelation.fieldPairs() )
for ( int i = 0; i < attrCount; ++i )
{
linkFeature.setAttribute( fieldPair.first, relatedFeature.attribute( fieldPair.second ) );
if ( i == firstIdx )
linkFeature.setAttribute( i, mFeature.attribute( mRelation.fieldPairs().at( 0 ).second ) );
else if ( i == secondIdx )
linkFeature.setAttribute( i, relatedFeature.attribute( mNmRelation.referencedFields().at( 0 ) ) );
else
linkFeature.setAttribute( i, mRelation.referencingLayer()->defaultValue( i ) );
}

newFeatures << linkFeature;
Expand Down

0 comments on commit 99a2f3b

Please sign in to comment.