Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Django Nose/sqlite3 "too many SQL variables" error

DZone's Guide to

Django Nose/sqlite3 "too many SQL variables" error

· Web Dev Zone
Free Resource

Learn how to build modern digital experience apps with Crafter CMS. Download this eBook now. Brought to you in partnership with Crafter Software

Trying to get started with Django nose today on an existing project, I kept getting the following error trying to run my empty test suite:

>./manage.py test --stop  
Creating test database for alias 'default'...  
...  
django.db.utils.DatabaseError: too many SQL variables 

 I noticed right away that this was only happening with sqlite3 as by database. When I switched to Postgres, everything worked. Wanting my unit tests to be as fast as possible, I still wanted to use sqlite3's in memory database. I had the following in my settings.py:

    if 'test' in sys.argv:  
        DATABASES = {  
            'default': {  
                'ENGINE': 'django.db.backends.sqlite3',  
                'NAME': ':memory',  
            },  
        }  

Searching around, I didn't find anyone else with this issue. I started doing a binary search on my models to find out if one of them was the culprit, but including more than 15 or so in any combination was enough to cause the issue. Eventually I set a break point in the nose code, and walked back up the stack to Django's create_permissions manage command, which in turn called bulk_create(). Basically, Django inserts a handful of permission records for each model you define, and in this case bulk_create was trying to pass more than 999 arguments into a SQL query, which SQLite doesn't allow.

Finally, I found open Django ticket 17788. It turns out this has already been fixed, albeit not in Django 1.4.0 final. Reading that patch, I was able to come up with a couple of lines of code you can put in tests.py to work-around the issue:

    # Needed to create Django permission records w/o triggering a  
    # "too many SQL variables" error, see: https://code.djangoproject.com/ticket/17788  
    from django.db.backends import sqlite3  
    sqlite3.base.DatabaseFeatures.can_combine_inserts_with_and_without_auto_increment_pk = False  

Crafter is a modern CMS platform for building modern websites and content-rich digital experiences. Download this eBook now. Brought to you in partnership with Crafter Software.

Topics:

Published at DZone with permission of Chase Seibert, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}