#21471 closed Bug (invalid)
CharField max_lenght is not enforced on SQLite backend with Python 3
Reported by: | lvella | Owned by: | nobody |
---|---|---|---|
Component: | Database layer (models, ORM) | Version: | 1.6 |
Severity: | Normal | Keywords: | |
Cc: | Triage Stage: | Unreviewed | |
Has patch: | no | Needs documentation: | no |
Needs tests: | no | Patch needs improvement: | no |
Easy pickings: | no | UI/UX: | no |
Description
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.
Change History (2)
comment:1 by , 11 years ago
Resolution: | → invalid |
---|---|
Status: | new → closed |
comment:2 by , 5 months ago
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]
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.