[Django] #27249: IntegrityError when using ManyToManyField.add due to type confusion

6 views
Skip to first unread message

Django

unread,
Sep 20, 2016, 4:36:19 AM9/20/16
to django-...@googlegroups.com
#27249: IntegrityError when using ManyToManyField.add due to type confusion
-------------------------------------+-------------------------------------
Reporter: sjoerdjob | Owner: nobody
Type: Uncategorized | Status: new
Component: Database layer | Version: master
(models, ORM) | Keywords: manytomanyfield,
Severity: Normal | integrityerror
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
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.

--
Ticket URL: <https://code.djangoproject.com/ticket/27249>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

Django

unread,
Sep 20, 2016, 2:26:35 PM9/20/16
to django-...@googlegroups.com
#27249: IntegrityError when using ManyToManyField.add() with a value of incorrect
type
-------------------------------------+-------------------------------------
Reporter: Sjoerd Job Postmus | Owner: nobody
Type: | Status: new
Cleanup/optimization |

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution:
Keywords: manytomanyfield, | Triage Stage: Accepted
integrityerror |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* needs_better_patch: => 0
* needs_tests: => 0
* needs_docs: => 0
* type: Uncategorized => Cleanup/optimization
* stage: Unreviewed => Accepted


Comment:

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

--
Ticket URL: <https://code.djangoproject.com/ticket/27249#comment:1>

Django

unread,
Sep 23, 2016, 10:55:55 AM9/23/16
to django-...@googlegroups.com
#27249: IntegrityError when using ManyToManyField.add() with a value of incorrect
type
-------------------------------------+-------------------------------------
Reporter: Sjoerd Job Postmus | Owner: nobody
Type: | Status: closed
Cleanup/optimization |

Component: Database layer | Version: master
(models, ORM) |
Severity: Normal | Resolution: duplicate

Keywords: manytomanyfield, | Triage Stage: Accepted
integrityerror |
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------
Changes (by Tim Graham):

* status: new => closed
* resolution: => duplicate


Comment:

Duplicate of #8467.

--
Ticket URL: <https://code.djangoproject.com/ticket/27249#comment:2>

Reply all
Reply to author
Forward
0 new messages