Skip to content

Commit

Permalink
Fix relation reference widget accidentally sets FK to NULL
Browse files Browse the repository at this point in the history
  • Loading branch information
m-kuhn committed Jul 24, 2015
1 parent d01424a commit 4eaf8de
Showing 1 changed file with 8 additions and 21 deletions.
29 changes: 8 additions & 21 deletions src/gui/editorwidgets/qgsrelationreferencewidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -245,24 +245,12 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
if ( !mReferencedLayer )
return;

QgsFeatureIterator fit;
QgsAttributes attrs = QgsAttributes( mReferencingLayer->pendingFields().count() );
attrs[mFkeyFieldIdx] = value;

// TODO: Rewrite using expression
if ( mMasterModel )
{
fit = mMasterModel->layerCache()->getFeatures( QgsFeatureRequest() );
}
else
{
fit = mReferencedLayer->getFeatures( QgsFeatureRequest() );
}
while ( fit.nextFeature( mFeature ) )
{
if ( mFeature.attribute( mFkeyFieldIdx ) == value )
{
break;
}
}
QgsFeatureRequest request = mRelation.getReferencedFeatureRequest( attrs );

mReferencedLayer->getFeatures( request ).nextFeature( mFeature );

if ( !mFeature.isValid() )
{
Expand All @@ -284,7 +272,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
}
else
{
int i = mComboBox->findData( value, QgsAttributeTableModel::FeatureIdRole );
int i = mComboBox->findData( mFeature.id(), QgsAttributeTableModel::FeatureIdRole );
if ( i == -1 && mAllowNull )
{
mComboBox->setCurrentIndex( 0 );
Expand Down Expand Up @@ -358,10 +346,9 @@ QVariant QgsRelationReferenceWidget::foreignKey()
}
else
{
QVariant varFid = mComboBox->itemData( mComboBox->currentIndex(), QgsAttributeTableModel::FeatureIdRole );
if ( varFid.isNull() )
if ( !mFeature.isValid() )
{
return QVariant();
return QVariant( mReferencingLayer->pendingFields().at( mFkeyFieldIdx ).type() );
}
else
{
Expand Down

0 comments on commit 4eaf8de

Please sign in to comment.