Skip to content

Commit

Permalink
[relations] Fix problematic polymorphic relation's generated relation…
Browse files Browse the repository at this point in the history
… IDs
  • Loading branch information
nirvn authored and nyalldawson committed Jun 19, 2024
1 parent 8d264ed commit b221228
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 2 deletions.
22 changes: 20 additions & 2 deletions src/core/qgspolymorphicrelation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,9 @@ QList<QgsRelation> QgsPolymorphicRelation::generateRelations() const
for ( const QString &referencedLayerId : referencedLayerIds )
{
QgsRelation relation;
QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->name();
const QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->name();

relation.setId( QStringLiteral( "%1_%2" ).arg( d->mRelationId, referencedLayerName ) );
relation.setId( QStringLiteral( "%1_%2" ).arg( d->mRelationId, referencedLayerId ) );
relation.setReferencedLayer( referencedLayerId );
relation.setReferencingLayer( d->mReferencingLayerId );
relation.setName( QStringLiteral( "Generated for \"%1\"" ).arg( referencedLayerName ) );
Expand All @@ -407,6 +407,24 @@ QList<QgsRelation> QgsPolymorphicRelation::generateRelations() const
return relations;
}

QString QgsPolymorphicRelation::upgradeGeneratedRelationId( const QString &oldRelationId ) const
{
if ( !isValid() )
return QString();

const QStringList referencedLayerIds = d->mReferencedLayerIds;
for ( const QString &referencedLayerId : referencedLayerIds )
{
const QString referencedLayerName = d->mReferencedLayersMap[referencedLayerId]->name();
if ( oldRelationId == QStringLiteral( "%1_%2" ).arg( d->mRelationId, referencedLayerName ) )
{
return QStringLiteral( "%1_%2" ).arg( d->mRelationId, referencedLayerId );
}
}

return QString();
}

QString QgsPolymorphicRelation::layerRepresentation( const QgsVectorLayer *layer ) const
{
if ( !layer || !layer->isValid() )
Expand Down
4 changes: 4 additions & 0 deletions src/core/qgspolymorphicrelation.h
Original file line number Diff line number Diff line change
Expand Up @@ -285,6 +285,10 @@ class CORE_EXPORT QgsPolymorphicRelation
void setRelationStrength( Qgis::RelationshipStrength relationStrength );

private:
friend class QgsRelationManager;

//! Upgrades a relation ID to a stabler ID former if generated using QGIS < 3.38
QString upgradeGeneratedRelationId( const QString &oldRelationId ) const;

QExplicitlySharedDataPointer<QgsPolymorphicRelationPrivate> d;

Expand Down
14 changes: 14 additions & 0 deletions src/core/qgsrelationmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,20 @@ void QgsRelationManager::removeRelation( const QgsRelation &relation )

QgsRelation QgsRelationManager::relation( const QString &id ) const
{
if ( !mRelations.contains( id ) )
{
// Check whether the provided ID refers to a polymorphic relation's generated ID
// from older versions of QGIS that used layer names instead of stable layer IDs.
const QList<QString> keys = mPolymorphicRelations.keys();
for ( const QString &key : keys )
{
if ( id.startsWith( key ) )
{
return mRelations.value( mPolymorphicRelations[key].upgradeGeneratedRelationId( id ) );
}
}
}

return mRelations.value( id );
}

Expand Down

0 comments on commit b221228

Please sign in to comment.