Skip to content

Commit 4eaf8de

Browse files
committedJul 24, 2015
Fix relation reference widget accidentally sets FK to NULL
1 parent d01424a commit 4eaf8de

File tree

1 file changed

+8
-21
lines changed

1 file changed

+8
-21
lines changed
 

‎src/gui/editorwidgets/qgsrelationreferencewidget.cpp

Lines changed: 8 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -245,24 +245,12 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
245245
if ( !mReferencedLayer )
246246
return;
247247

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

250-
// TODO: Rewrite using expression
251-
if ( mMasterModel )
252-
{
253-
fit = mMasterModel->layerCache()->getFeatures( QgsFeatureRequest() );
254-
}
255-
else
256-
{
257-
fit = mReferencedLayer->getFeatures( QgsFeatureRequest() );
258-
}
259-
while ( fit.nextFeature( mFeature ) )
260-
{
261-
if ( mFeature.attribute( mFkeyFieldIdx ) == value )
262-
{
263-
break;
264-
}
265-
}
251+
QgsFeatureRequest request = mRelation.getReferencedFeatureRequest( attrs );
252+
253+
mReferencedLayer->getFeatures( request ).nextFeature( mFeature );
266254

267255
if ( !mFeature.isValid() )
268256
{
@@ -284,7 +272,7 @@ void QgsRelationReferenceWidget::setForeignKey( const QVariant& value )
284272
}
285273
else
286274
{
287-
int i = mComboBox->findData( value, QgsAttributeTableModel::FeatureIdRole );
275+
int i = mComboBox->findData( mFeature.id(), QgsAttributeTableModel::FeatureIdRole );
288276
if ( i == -1 && mAllowNull )
289277
{
290278
mComboBox->setCurrentIndex( 0 );
@@ -358,10 +346,9 @@ QVariant QgsRelationReferenceWidget::foreignKey()
358346
}
359347
else
360348
{
361-
QVariant varFid = mComboBox->itemData( mComboBox->currentIndex(), QgsAttributeTableModel::FeatureIdRole );
362-
if ( varFid.isNull() )
349+
if ( !mFeature.isValid() )
363350
{
364-
return QVariant();
351+
return QVariant( mReferencingLayer->pendingFields().at( mFkeyFieldIdx ).type() );
365352
}
366353
else
367354
{

0 commit comments

Comments
 (0)
Please sign in to comment.