[Django] #21471: CharField max_lenght is not enforced on SQLite backend with Python 3

17 views
Skip to first unread message

Django

unread,
Nov 19, 2013, 9:57:50 AM11/19/13
to django-...@googlegroups.com
#21471: CharField max_lenght is not enforced on SQLite backend with Python 3
----------------------------------------------+--------------------
Reporter: lvella | Owner: nobody
Type: Bug | Status: new
Component: Database layer (models, ORM) | Version: 1.6
Severity: Normal | Keywords:
Triage Stage: Unreviewed | Has patch: 0
Easy pickings: 0 | UI/UX: 0
----------------------------------------------+--------------------
I am using Django 1.6 with CPython 3.3.2. The minimal test case to show
the bug is as follows:

From a Django project using SQLite, create an app:

$ python3 manage.py startapp case

Add the following contents to 'case/models.py':

from django.db import models

class A(models.Model):
b = models.CharField(max_length=10)

def __str__(self):
return self.b

Then, after adding the app to INSTALLED_APPS setting, sync the database:

$ python3 manage.py syncdb

Then run:
$ python3 manage.py shell
>>> from case.models import A
>>> a = A(b='x'*20)
>>> a.save()
>>> A.objects.all()[0]
<A: xxxxxxxxxxxxxxxxxxxx>

If you could reproduce, the 20 characters string was allowed on a
CharField with max_length=10. This caused me problems because I user
SQLite on development, and PostgreSQL on production, and because of this
issue, I missed a bug during development only to see it in production.

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

Django

unread,
Nov 19, 2013, 10:14:44 AM11/19/13
to django-...@googlegroups.com
#21471: CharField max_lenght is not enforced on SQLite backend with Python 3
-------------------------------------+-------------------------------------
Reporter: lvella | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.6
(models, ORM) | Resolution: invalid
Severity: Normal | Triage Stage:
Keywords: | Unreviewed
Has patch: 0 | Needs documentation: 0
Needs tests: 0 | Patch needs improvement: 0

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

* status: new => closed
* needs_docs: => 0
* resolution: => invalid
* needs_tests: => 0
* needs_better_patch: => 0


Comment:

Yes, this is a limitation of SQLite. see http://www.sqlite.org/faq.html#q9

This is why it's recommended to use the same database for development as
you use in production.

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

Django

unread,
Jul 22, 2024, 3:04:56 AM7/22/24
to django-...@googlegroups.com
#21471: CharField max_lenght is not enforced on SQLite backend with Python 3
-------------------------------------+-------------------------------------
Reporter: lvella | Owner: nobody
Type: Bug | Status: closed
Component: Database layer | Version: 1.6
(models, ORM) |
Severity: Normal | Resolution: invalid
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 vivook):

This could be done with a check constraint. [See discussion
https://forum.djangoproject.com/t/idea-make-sqlite-enforce-varchar-
lengths-via-check-constraints/33101]
--
Ticket URL: <https://code.djangoproject.com/ticket/21471#comment:2>
Reply all
Reply to author
Forward
0 new messages