Opened 2 years ago

Closed 2 years ago

#27249 closed Cleanup/optimization (duplicate)

IntegrityError when using ManyToManyField.add() with a value of incorrect type

Reported by: Sjoerd Job Postmus Owned by: nobody
Component: Database layer (models, ORM) Version: master
Severity: Normal Keywords: manytomanyfield, integrityerror
Cc: Triage Stage: Accepted
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


When specifying the primary keys of objects to related_manager.add, one has to use the same type as the database returns, at the risk of getting an IntegrityError.

Example (using Django contrib.auth).

>>> from django.contrib.auth.models import Group, Permission
>>> group = Group.objects.create()
>>> permission = Permission.objects.first()
>>> print(
>>> group.permissions.add(
>>> group.permissions.add(
>>> group.permissions.add(str(
Traceback (most recent call last):
... <snip> ...
IntegrityError: UNIQUE constraint failed: auth_group_permissions.group_id, auth_group_permissions.permission_id
>>> group.permissions.add(
>>> group.permissions.add(

Now of course, I assume nobody would do an explicit call to str() there, but if the primary keys come from another input source (like: a URL), it is not unlikely to expect them to not be the same type as the database uses.

(seen in 1.9.6, 1.10.1 and master).

Not sure about how to fix it, because the type of the primary key might not necessarily be an integer.

Change History (2)

comment:1 Changed 2 years ago by Tim Graham

Summary: IntegrityError when using ManyToManyField.add due to type confusionIntegrityError when using ManyToManyField.add() with a value of incorrect type
Triage Stage: UnreviewedAccepted
Type: UncategorizedCleanup/optimization

I guess as long as the performance penalty isn't too large, a possibility could be to call Field.to_python() on the input.

comment:2 Changed 2 years ago by Tim Graham

Resolution: duplicate
Status: newclosed

Duplicate of #8467.

Note: See TracTickets for help on using tickets.
Back to Top