Skip to content

Commit e5187b2

Browse files
committedNov 7, 2017
Respect default values in N:M relations
1 parent 2eddb03 commit e5187b2

File tree

1 file changed

+13
-3
lines changed

1 file changed

+13
-3
lines changed
 

‎src/gui/qgsrelationeditorwidget.cpp

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "qgsproject.h"
2929
#include "qgstransactiongroup.h"
3030
#include "qgslogger.h"
31+
#include "qgsvectorlayerutils.h"
3132

3233
#include <QHBoxLayout>
3334
#include <QLabel>
@@ -351,19 +352,28 @@ void QgsRelationEditorWidget::linkFeature()
351352
QgsFeature relatedFeature;
352353

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

356+
// Fields of the linking table
357+
const QgsFields fields = mRelation.referencingLayer()->fields();
358+
359+
// Expression context for the linking table
360+
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();
361+
362+
QgsAttributeMap linkAttributes;
356363
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation.fieldPairs() )
357364
{
358-
linkFeature.setAttribute( fieldPair.first, mFeature.attribute( fieldPair.second ) );
365+
int index = fields.indexOf( fieldPair.first );
366+
linkAttributes.insert( index, mFeature.attribute( fieldPair.second ) );
359367
}
360368

361369
while ( it.nextFeature( relatedFeature ) )
362370
{
363371
Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation.fieldPairs() )
364372
{
365-
linkFeature.setAttribute( fieldPair.first, relatedFeature.attribute( fieldPair.second ) );
373+
int index = fields.indexOf( fieldPair.first );
374+
linkAttributes.insert( index, relatedFeature.attribute( fieldPair.second ) );
366375
}
376+
const QgsFeature linkFeature = QgsVectorLayerUtils::createFeature( mRelation.referencingLayer(), QgsGeometry(), linkAttributes, &context );
367377

368378
newFeatures << linkFeature;
369379
}

0 commit comments

Comments
 (0)
Please sign in to comment.