@@ -306,12 +306,27 @@ void QgsRelationEditorWidget::addFeature()
306
306
QgsFeature f;
307
307
if ( vlTools->addFeature ( mNmRelation .referencedLayer (), QgsAttributeMap (), QgsGeometry (), &f ) )
308
308
{
309
- QgsFeature flink ( mRelation .referencingLayer ()->fields () ); // Linking feature
309
+ // Fields of the linking table
310
+ const QgsFields fields = mRelation .referencingLayer ()->fields ();
311
+
312
+ // Expression context for the linking table
313
+ QgsExpressionContext context = mRelation .referencingLayer ()->createExpressionContext ();
310
314
311
- flink.setAttribute ( mRelation .fieldPairs ().at ( 0 ).first , mFeature .attribute ( mRelation .fieldPairs ().at ( 0 ).second ) );
312
- flink.setAttribute ( mNmRelation .referencingFields ().at ( 0 ), f.attribute ( mNmRelation .referencedFields ().at ( 0 ) ) );
315
+ QgsAttributeMap linkAttributes;
316
+ Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mRelation .fieldPairs () )
317
+ {
318
+ int index = fields.indexOf ( fieldPair.first );
319
+ linkAttributes.insert ( index, mFeature .attribute ( fieldPair.second ) );
320
+ }
321
+
322
+ Q_FOREACH ( const QgsRelation::FieldPair &fieldPair, mNmRelation .fieldPairs () )
323
+ {
324
+ int index = fields.indexOf ( fieldPair.first );
325
+ linkAttributes.insert ( index, f.attribute ( fieldPair.second ) );
326
+ }
327
+ QgsFeature linkFeature = QgsVectorLayerUtils::createFeature ( mRelation .referencingLayer (), QgsGeometry (), linkAttributes, &context );
313
328
314
- mRelation .referencingLayer ()->addFeature ( flink );
329
+ mRelation .referencingLayer ()->addFeature ( linkFeature );
315
330
316
331
updateUi ();
317
332
}
0 commit comments