@@ -213,6 +213,16 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
213
213
214
214
const QgsVectorLayerTools *vlTools = mEditorContext .vectorLayerTools ();
215
215
216
+ // Fields of the linking table
217
+ const QgsFields fields = mRelation .referencingLayer ()->fields ();
218
+
219
+ // For generated relations insert the referenced layer field
220
+ if ( mRelation .type () == QgsRelation::Generated )
221
+ {
222
+ QgsPolymorphicRelation polyRel = mRelation .polymorphicRelation ();
223
+ keyAttrs.insert ( fields.indexFromName ( polyRel.referencedLayerField () ), polyRel.layerRepresentation ( mRelation .referencedLayer () ) );
224
+ }
225
+
216
226
if ( mNmRelation .isValid () )
217
227
{
218
228
// only normal relations support m:n relation
@@ -224,13 +234,10 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
224
234
if ( !vlTools->addFeature ( mNmRelation .referencedLayer (), QgsAttributeMap (), geometry, &f ) )
225
235
return ;
226
236
227
- // Fields of the linking table
228
- const QgsFields fields = mRelation .referencingLayer ()->fields ();
229
-
230
237
// Expression context for the linking table
231
238
QgsExpressionContext context = mRelation .referencingLayer ()->createExpressionContext ();
232
239
233
- QgsAttributeMap linkAttributes;
240
+ QgsAttributeMap linkAttributes = keyAttrs ;
234
241
const auto constFieldPairs = mRelation .fieldPairs ();
235
242
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
236
243
{
@@ -250,13 +257,6 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
250
257
}
251
258
else
252
259
{
253
- QgsFields fields = mRelation .referencingLayer ()->fields ();
254
- if ( mRelation .type () == QgsRelation::Generated )
255
- {
256
- QgsPolymorphicRelation polyRel = mRelation .polymorphicRelation ();
257
- keyAttrs.insert ( fields.indexFromName ( polyRel.referencedLayerField () ), polyRel.layerRepresentation ( mRelation .referencedLayer () ) );
258
- }
259
-
260
260
const auto constFieldPairs = mRelation .fieldPairs ();
261
261
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
262
262
{
@@ -440,6 +440,16 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
440
440
QgsExpressionContext context = mRelation .referencingLayer ()->createExpressionContext ();
441
441
442
442
QgsAttributeMap linkAttributes;
443
+
444
+ if ( mRelation .type () == QgsRelation::Generated )
445
+ {
446
+ QgsPolymorphicRelation polyRel = mRelation .polymorphicRelation ();
447
+ Q_ASSERT ( polyRel.isValid () );
448
+
449
+ linkAttributes.insert ( fields.indexFromName ( polyRel.referencedLayerField () ),
450
+ polyRel.layerRepresentation ( mRelation .referencedLayer () ) );
451
+ }
452
+
443
453
const auto constFieldPairs = mRelation .fieldPairs ();
444
454
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
445
455
{
@@ -486,7 +496,7 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
486
496
{
487
497
QgsPolymorphicRelation polyRel = mRelation .polymorphicRelation ();
488
498
489
- Q_ASSERT ( mRelation . polymorphicRelation () .isValid () );
499
+ Q_ASSERT ( polyRel .isValid () );
490
500
491
501
mRelation .referencingLayer ()->changeAttributeValue ( fid,
492
502
referencingLayer->fields ().indexFromName ( polyRel.referencedLayerField () ),
0 commit comments