﻿id	summary	reporter	owner	description	type	status	component	version	severity	resolution	keywords	cc	stage	has_patch	needs_docs	needs_tests	needs_better_patch	easy	ui_ux
27249	IntegrityError when using ManyToManyField.add() with a value of incorrect type	Sjoerd Job Postmus	nobody	"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(permission.pk)
1
>>> group.permissions.add(permission.pk)
>>> group.permissions.add(permission.pk)
>>> group.permissions.add(str(permission.pk))
Traceback (most recent call last):
... <snip> ...
IntegrityError: UNIQUE constraint failed: auth_group_permissions.group_id, auth_group_permissions.permission_id
>>> group.permissions.add(permission.pk)
>>> group.permissions.add(permission.pk)
}}}

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."	Cleanup/optimization	closed	Database layer (models, ORM)	dev	Normal	duplicate	manytomanyfield, integrityerror		Accepted	0	0	0	0	0	0
