Skip to content
Permalink
Browse files

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.
  • Loading branch information
domi4484 authored and nyalldawson committed Mar 28, 2021
1 parent 555f516 commit c3771b00f05315d345308598aa0f2f3fdeb28ddd
Showing with 22 additions and 12 deletions.
  1. +22 −12 src/gui/qgsabstractrelationeditorwidget.cpp
@@ -213,6 +213,16 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )

const QgsVectorLayerTools *vlTools = mEditorContext.vectorLayerTools();

// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();

// For generated relations insert the referenced layer field
if ( mRelation.type() == QgsRelation::Generated )
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
keyAttrs.insert( fields.indexFromName( polyRel.referencedLayerField() ), polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

if ( mNmRelation.isValid() )
{
// only normal relations support m:n relation
@@ -224,13 +234,10 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
if ( !vlTools->addFeature( mNmRelation.referencedLayer(), QgsAttributeMap(), geometry, &f ) )
return;

// Fields of the linking table
const QgsFields fields = mRelation.referencingLayer()->fields();

// Expression context for the linking table
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();

QgsAttributeMap linkAttributes;
QgsAttributeMap linkAttributes = keyAttrs;
const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
@@ -250,13 +257,6 @@ void QgsAbstractRelationEditorWidget::addFeature( const QgsGeometry &geometry )
}
else
{
QgsFields fields = mRelation.referencingLayer()->fields();
if ( mRelation.type() == QgsRelation::Generated )
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
keyAttrs.insert( fields.indexFromName( polyRel.referencedLayerField() ), polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
@@ -440,6 +440,16 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
QgsExpressionContext context = mRelation.referencingLayer()->createExpressionContext();

QgsAttributeMap linkAttributes;

if ( mRelation.type() == QgsRelation::Generated )
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();
Q_ASSERT( polyRel.isValid() );

linkAttributes.insert( fields.indexFromName( polyRel.referencedLayerField() ),
polyRel.layerRepresentation( mRelation.referencedLayer() ) );
}

const auto constFieldPairs = mRelation.fieldPairs();
for ( const QgsRelation::FieldPair &fieldPair : constFieldPairs )
{
@@ -486,7 +496,7 @@ void QgsAbstractRelationEditorWidget::onLinkFeatureDlgAccepted()
{
QgsPolymorphicRelation polyRel = mRelation.polymorphicRelation();

Q_ASSERT( mRelation.polymorphicRelation().isValid() );
Q_ASSERT( polyRel.isValid() );

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

0 comments on commit c3771b0

Please sign in to comment.