[Django] #29764: in_bulk does not works with composed unique field

5 views
Skip to first unread message

Django

unread,
Sep 17, 2018, 10:17:45 AM9/17/18
to django-...@googlegroups.com
#29764: in_bulk does not works with composed unique field
-------------------------------------+-------------------------------------
Reporter: Jean- | Owner: nobody
Daniel |
Type: Bug | Status: new
Component: Database | Version: 2.1
layer (models, ORM) |
Severity: Normal | Keywords:
Triage Stage: | Has patch: 0
Unreviewed |
Needs documentation: 0 | Needs tests: 0
Patch needs improvement: 0 | Easy pickings: 0
UI/UX: 0 |
-------------------------------------+-------------------------------------
For instance, I have a model 'Data' with an unique_together constraint on
(foo, bar) fields.

I would like to use in_bulk() on a query like:

{{{
Data.objects.filter(foo='a').in_bulk(<list of bar values>,
field_name='bar').
}}}

Obviously, the fact that bar is not unique should not be an issue to run
the query, but Django performs a check to make sure the queried field is
unique and raise an error if not.

As it may not be simple to check for all cases where field_name would
result in a unique constraint, maybe it should be possible to disable this
check (using an optional parameter).

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

Django

unread,
Sep 17, 2018, 4:42:52 PM9/17/18
to django-...@googlegroups.com
#29764: in_bulk does not works with composed unique field
-------------------------------------+-------------------------------------
Reporter: Jean-Daniel | Owner: nobody
Type: Bug | Status: new
Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Tim Graham):

In your example, if bar isn't unique, then values may be overwritten in
the `in_bulk()` return value unless the keys become `('foo', 'bar')` (but
how would `in_bulk()` know that?). I think this proposal would complicate
`in_bulk()` a bit too much.

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

Django

unread,
Sep 17, 2018, 6:17:20 PM9/17/18
to django-...@googlegroups.com
#29764: in_bulk does not works with composed unique field
-------------------------------------+-------------------------------------
Reporter: Jean-Daniel | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Jean-Daniel):

Replying to [comment:1 Tim Graham]:


> In your example, if bar isn't unique, then values may be overwritten in
the `in_bulk()` return value unless the keys become `('foo', 'bar')` (but
how would `in_bulk()` know that?). I think this proposal would complicate
`in_bulk()` a bit too much.

In my example, bar is guarantee to be unique, as the query already
contains an equality filter on foo. And yes, I understand it would make
the function too complex to try to handle all possibles cases, that's why
I suggested to let the caller choose to disable the check, ie something
like this:

{{{
in_bulk(<list of bar>, field_name='bar', disable_unique_check=True)
}}}

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

Django

unread,
Sep 17, 2018, 6:54:19 PM9/17/18
to django-...@googlegroups.com
#29764: in_bulk does not works with composed unique field
-------------------------------------+-------------------------------------
Reporter: Jean-Daniel | Owner: nobody
Type: Bug | Status: new

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution:
Keywords: | Triage Stage:
| Unreviewed
Has patch: 0 | Needs documentation: 0

Needs tests: 0 | Patch needs improvement: 0
Easy pickings: 0 | UI/UX: 0
-------------------------------------+-------------------------------------

Comment (by Simon Charette):

Given `in_bulk` can be easily replaced with a dict literal I don't think
it's worth complexifying it more for the rare cases where this is useful
either.

{{{#!python
bulk_data = {
data.bar: data for data Data.objects.filter(foo='a')
}

--
Ticket URL: <https://code.djangoproject.com/ticket/29764#comment:3>

Django

unread,
Sep 18, 2018, 9:02:36 AM9/18/18
to django-...@googlegroups.com
#29764: Allow using QuerySet.in_bulk() with a composed unique field
-------------------------------------+-------------------------------------
Reporter: Jean-Daniel | Owner: nobody
Type: New feature | Status: closed

Component: Database layer | Version: 2.1
(models, ORM) |
Severity: Normal | Resolution: wontfix
Keywords: | Triage Stage:
| Unreviewed
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: => wontfix
* type: Bug => New feature


--
Ticket URL: <https://code.djangoproject.com/ticket/29764#comment:4>

Reply all
Reply to author
Forward
0 new messages