For ManyToMany manager, we should convert objects being added or removed to the pk type if they are not.
|Reported by:||Wonlay||Owned by:||nobody|
|Component:||Database layer (models, ORM)||Version:||master|
|Severity:||Normal||Keywords:||Duplicate entry, add, remove, ManyToManyField|
|Has patch:||yes||Needs documentation:||no|
|Needs tests:||yes||Patch needs improvement:||yes|
class A(models.Model): name = models.CharField(blank=True) class B(models.Model): a = models.ManyToManyField(A) a = A(name='aaa') a.save() b = B() b.save() # This line works fine: b.a.add('1') # But this one will raise 'Duplicate entry' error: b.a.add('1')
This is caused by '1' is not an integer, the duplication checking code in add() fails when checking the string type '1'.
The same problem is applied to the remove() method.
If we convert the objects to its pk type of the model, code will run correctly.
And my patch is attached.
Change History (12)
Changed 7 years ago by Wonlay
comment:1 Changed 7 years ago by ubernostrum
- milestone changed from 1.0 to post-1.0
- Needs documentation unset
- Needs tests unset
- Patch needs improvement unset
comment:3 Changed 7 years ago by jacob
- Triage Stage changed from Unreviewed to Design decision needed
comment:7 Changed 3 years ago by aaugustin
- Component changed from Core (Other) to Database layer (models, ORM)
comment:8 Changed 3 years ago by akaariai
- Triage Stage changed from Design decision needed to Accepted