diff --git a/src/core/qgsofflineediting.cpp b/src/core/qgsofflineediting.cpp index 76646567470..76dc44c6fdd 100644 --- a/src/core/qgsofflineediting.cpp +++ b/src/core/qgsofflineediting.cpp @@ -254,6 +254,7 @@ void QgsOfflineEditing::synchronize() // copy style copySymbology( offlineLayer, remoteLayer ); + updateRelations( offlineLayer, remoteLayer ); // apply layer edit log QString qgisLayerId = layer->id(); @@ -611,6 +612,7 @@ QgsVectorLayer* QgsOfflineEditing::copyVectorLayer( QgsVectorLayer* layer, sqlit } } + updateRelations( layer, newLayer ); // copy features newLayer->startEditing(); QgsFeature f; @@ -898,6 +900,29 @@ void QgsOfflineEditing::copySymbology( QgsVectorLayer* sourceLayer, QgsVectorLay } } +void QgsOfflineEditing::updateRelations( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer ) +{ + QgsRelationManager* relationManager = QgsProject::instance()->relationManager(); + QList relations; + relations = relationManager->referencedRelations( sourceLayer ); + + Q_FOREACH ( QgsRelation relation, relations ) + { + relationManager->removeRelation( relation ); + relation.setReferencedLayer( targetLayer->id() ); + relationManager->addRelation( relation ); + } + + relations = relationManager->referencingRelations( sourceLayer ); + + Q_FOREACH ( QgsRelation relation, relations ) + { + relationManager->removeRelation( relation ); + relation.setReferencingLayer( targetLayer->id() ); + relationManager->addRelation( relation ); + } +} + // NOTE: use this to map column indices in case the remote geometry column is not last QMap QgsOfflineEditing::attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer ) { diff --git a/src/core/qgsofflineediting.h b/src/core/qgsofflineediting.h index a44b4c894fe..4425a1832df 100644 --- a/src/core/qgsofflineediting.h +++ b/src/core/qgsofflineediting.h @@ -108,6 +108,11 @@ class CORE_EXPORT QgsOfflineEditing : public QObject void applyGeometryChanges( QgsVectorLayer* remoteLayer, sqlite3* db, int layerId, int commitNo ); void updateFidLookup( QgsVectorLayer* remoteLayer, sqlite3* db, int layerId ); void copySymbology( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer ); + + /** + * Updates all relations that reference or are referenced by the source layer to the targetLayer. + */ + void updateRelations( QgsVectorLayer* sourceLayer, QgsVectorLayer* targetLayer ); QMap attributeLookup( QgsVectorLayer* offlineLayer, QgsVectorLayer* remoteLayer ); void showWarning( const QString& message );