bug: SetField merge with append=True introduces duplicate values
Name
SetField merge with append=True introduces duplicate values
Analyzers
No response
Connectors
No response
Runtime configuration
File: api_app/data_model_manager/models.py, line 146
Commit: 862df0a0
The merge() method iterates over all model fields and, for any ArrayField, extends the result list with values from the other instance:
if isinstance(field, ArrayField):
if not result_attr:
result_attr = []
result_attr.extend(other_attr)
SetField is a subclass of ArrayField. The isinstance(field, ArrayField) check at line 146 therefore matches SetField fields. The extend() call appends all elements from other_attr with no deduplication.
SetField's uniqueness enforcement lives in to_python(), which Django calls only during form validation — not during setattr + save(). PostgreSQL's ArrayField does not enforce uniqueness at the database level either. Duplicates produced by extend() are therefore persisted as-is.
Use case
When two model instances share a value in a SetField column (e.g., both IPDataModel records have '1.1.1.1' in their resolutions field) and merge(append=True) is called, the merged result contains duplicates: ['1.1.1.1', '1.1.1.1', '2.2.2.2'] instead of ['1.1.1.1', '2.2.2.2']. This violates SetField's uniqueness contract and the duplicate values are persisted to the database.
Steps to reproduce:
- Create two
IPDataModelinstances with overlappingresolutionsvalues — e.g., both containing'1.1.1.1'. - Call
merge(append=True)on the first instance. - Call
refresh_from_db()on the first instance. - Inspect
len(resolutions)— it is3instead of the expected2.
Reproducing test
Add to tests/api_app/data_model_manager/test_models.py. This test fails on the current code and is proof the bug is real:
def test_merge_set_field_no_duplicates(self):
ip1 = IPDataModel.objects.create(resolutions=['1.1.1.1'])
ip2 = IPDataModel.objects.create(resolutions=['1.1.1.1', '2.2.2.2'])
ip1.merge(ip2, append=True)
ip1.refresh_from_db()
# SetField must preserve uniqueness: ['1.1.1.1', '2.2.2.2'], NOT ['1.1.1.1', '1.1.1.1', '2.2.2.2']
self.assertEqual(len(ip1.resolutions), 2) # FAILS on buggy code: actual len is 3
self.assertCountEqual(ip1.resolutions, ['1.1.1.1', '2.2.2.2'])
ip1.delete()
ip2.delete()
Hi @mlodic, I would like to work on this issue as a first-time contributor. Could you please assign it to me?
I have checked and confirmed that the bug is real.
Root Cause
- In api_app/data_model_manager/fields.py, SetField inherits directly from ArrayField (class SetField(ArrayField)).
- In api_app/data_model_manager/models.py, merge() checks isinstance(field, ArrayField) at line 147 before isinstance(field, (JSONField, SetField)) at line 151.
- Because SetField is a subclass of ArrayField, isinstance(field, ArrayField) evaluates to True, so result_attr.extend(other_attr) executes without deduplication, and the SetField branch is never reached.
- SetField deduplication (to_python) is only called during Django form validation, so duplicate list elements are persisted directly to the database. @mlodic i would like to work on this issue please assign it to me
This issue has been marked as stale because it has had no activity for 30 days. If you are still working on this, please provide some updates.