Skip to content
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions app/attributes/attributecontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,13 +129,33 @@ void AttributeController::prefillRelationReferenceField()
{
QVariant foreignKey = mParentController->featureLayerPair().feature().attribute( fieldPair.referencedField() );
QString referencingField = fieldPair.referencingField();
const QgsVectorLayer *childLayer = mLinkedRelation.referencingLayer();
if ( childLayer )

// set the foreign key via FormItem, so that the UI and validation are updated
bool setViaFormItem = false;
QMap<QUuid, std::shared_ptr<FormItem>>::iterator formItemsIterator = mFormItems.begin();
while ( formItemsIterator != mFormItems.end() )
{
std::shared_ptr<FormItem> itemData = formItemsIterator.value();
if ( itemData->field().name() == referencingField )
{
setFormValue( itemData->id(), foreignKey );
setViaFormItem = true;
break;
}
++formItemsIterator;
}

// if the field is not in the displayed in the form, set the attribute directly on the feature
if ( !setViaFormItem )
{
const int fieldIndex = childLayer->fields().lookupField( referencingField );
if ( fieldIndex != -1 )
const QgsVectorLayer *childLayer = mLinkedRelation.referencingLayer();
if ( childLayer )
{
mFeatureLayerPair.featureRef().setAttribute( fieldIndex, foreignKey );
const int fieldIndex = childLayer->fields().lookupField( referencingField );
if ( fieldIndex != -1 )
{
mFeatureLayerPair.featureRef().setAttribute( fieldIndex, foreignKey );
}
}
}
}
Expand Down
Loading