DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations
  1. DZone
  2. Data Engineering
  3. Databases
  4. Django Nose/sqlite3 "too many SQL variables" error

Django Nose/sqlite3 "too many SQL variables" error

Chase Seibert user avatar by
Chase Seibert
·
Jun. 07, 12 · Interview
Like (0)
Save
Tweet
Share
5.03K Views

Join the DZone community and get the full member experience.

Join For Free

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  
Django (web framework) sql

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

Opinions expressed by DZone contributors are their own.

Popular on DZone

  • Choosing the Right Framework for Your Project
  • Using GPT-3 in Our Applications
  • Introduction to Spring Cloud Kubernetes
  • Shift-Left: A Developer's Pipe(line) Dream?

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com
  • +1 (919) 678-0300

Let's be friends: