Skip to content

Commit 061aeb8

Browse files
domi44843nids
authored andcommittedMar 29, 2021
Fix AbstractRelationEditorWidget not updating/inserting referenced layer
QgsAbstractRelationEditorWidget need to update/insert the referenced layer field also when an nm relation is used. This was not the case for addFeature and linkFeature methods.
1 parent d856156 commit 061aeb8

File tree

1 file changed

+22
-12
lines changed

1 file changed

+22
-12
lines changed
 

‎src/gui/qgsabstractrelationeditorwidget.cpp

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -213,6 +213,16 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
213213

214214
const QgsVectorLayerTools *vlTools = mEditorContext.vectorLayerTools();
215215

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+
216226
if ( mNmRelation.isValid() )
217227
{
218228
// only normal relations support m:n relation
@@ -224,13 +234,10 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
224234
if ( !vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), geometry, &f ) )
225235
return;
226236

227-
// Fields of the linking table
228-
const QgsFields fields = mRelation.referencingLayer()->fields();
229-
230237
// Expression context for the linking table
231238
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();
232239

233-
QgsAttributeMap linkAttributes;
240+
QgsAttributeMap linkAttributes = keyAttrs;
234241
const auto constFieldPairs = mRelation.fieldPairs();
235242
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
236243
{
@@ -250,13 +257,6 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
250257
}
251258
else
252259
{
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-
260260
const auto constFieldPairs = mRelation.fieldPairs();
261261
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
262262
{
@@ -440,6 +440,16 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
440440
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();
441441

442442
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+
443453
const auto constFieldPairs = mRelation.fieldPairs();
444454
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
445455
{
@@ -486,7 +496,7 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
486496
{
487497
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
488498

489-
Q_ASSERT( mRelation.polymorphicRelation().isValid() );
499+
Q_ASSERT( polyRel.isValid() );
490500

491501
mRelation.referencingLayer()->changeAttributeValue( fid,
492502
referencingLayer->fields().indexFromName( polyRel.referencedLayerField() ),

0 commit comments

Comments
 (0)
Please sign in to comment.