Opened 21 months ago

Closed 4 months ago

Last modified 4 months ago

#22705 closed Bug (fixed)

can't bulk create models with no fields

Reported by: ellery-newcomer@… Owned by: chrisjluc
Component: Database layer (models, ORM) Version: 1.6
Severity: Normal Keywords: QuerySet.bulk_create
Cc: Triage Stage: Ready for checkin
Has patch: yes Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no

Description

I have a model

class Discussion(models.Model): pass

I try to do this:

discussions = [Discussion() for i in range(n)]
Discussion.objects.bulk_create(discussions)

it fails with

AttributeError: 'NoneType' object has no attribute 'column'

because

django.db.models.sql.compiler.SQLInsertCompiler.as_sql has the following line in it:

fields = [None]

which doesn't get tripped on models that have fields, apparently.

Attachments (1)

22705-test.diff (1.2 KB) - added by timo 21 months ago.

Download all attachments as: .zip

Change History (9)

comment:1 Changed 21 months ago by timo

  • Component changed from Uncategorized to Database layer (models, ORM)
  • Needs documentation unset
  • Needs tests unset
  • Patch needs improvement unset
  • Triage Stage changed from Unreviewed to Accepted
  • Type changed from Uncategorized to Bug

Can reproduce with the attached test for Django's test suite.

Changed 21 months ago by timo

comment:2 Changed 11 months ago by chrisjluc

  • Owner changed from nobody to chrisjluc
  • Status changed from new to assigned

comment:3 Changed 10 months ago by chrisjluc

I've modified the compiler to create the below query, but I'm getting an Integrity Error: datatype mismatch when I run it in Django. I assume it's because id is a pk and it doesn't accept NULL values, but it seems to work on my own sqlite. Does anyone know how to solve this issue?

We use the second form of bulk inserting, ref https://www.sqlite.org/lang_insert.html

u'INSERT INTO "bulk_create_nofields" ("id") SELECT %s UNION ALL SELECT %s UNION ALL SELECT %s', (u'NULL', u'NULL', u'NULL'))

Traceback (most recent call last):
  File "/Users/chrisjluc/Documents/opensource/django/tests/bulk_create/tests.py", line 172, in test_empty_model
    NoFields.objects.bulk_create(objs)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/models/query.py", line 445, in bulk_create
    self._batched_insert(objs_without_pk, fields, batch_size)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/models/query.py", line 1043, in _batched_insert
    using=self.db)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/models/manager.py", line 127, in manager_method
    return getattr(self.get_queryset(), name)(*args, **kwargs)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/models/query.py", line 1026, in _insert
    return query.get_compiler(using=using).execute_sql(return_id)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/models/sql/compiler.py", line 974, in execute_sql
    cursor.execute(sql, params)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/utils.py", line 95, in __exit__
    six.reraise(dj_exc_type, dj_exc_value, traceback)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/backends/utils.py", line 64, in execute
    return self.cursor.execute(sql, params)
  File "/Users/chrisjluc/Documents/opensource/django/django/db/backends/sqlite3/base.py", line 318, in execute
    return Database.Cursor.execute(self, query, params)
IntegrityError: datatype mismatch

comment:4 Changed 6 months ago by timgraham

  • Keywords QuerySet.bulk_create added

comment:5 Changed 4 months ago by timgraham

  • Has patch set

Fixed in Django 1.9 by 134ca4d438bd7cbe8f0f287a00d545f96fa04a01.

PR to add a regression test.

comment:6 Changed 4 months ago by charettes

  • Triage Stage changed from Accepted to Ready for checkin

Regression test LGTM.

comment:7 Changed 4 months ago by Tim Graham <timograham@…>

  • Resolution set to fixed
  • Status changed from assigned to closed

In 7a5b7e35:

Fixed #22705 -- Fixed QuerySet.bulk_create() on models without any fields on Oracle.

Fixed on other backends by 134ca4d438bd7cbe8f0f287a00d545f96fa04a01.

Thanks Mariusz Felisiak for the solution.

comment:8 Changed 4 months ago by Tim Graham <timograham@…>

In 7cd2995:

[1.9.x] Fixed #22705 -- Fixed QuerySet.bulk_create() on models without any fields on Oracle.

Fixed on other backends by 134ca4d438bd7cbe8f0f287a00d545f96fa04a01.

Thanks Mariusz Felisiak for the solution.

Backport of 7a5b7e35bf2e219225b9f26d3dd3e34f26e83e9c from master

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