# upgrading from old Django version 2.2. add PositiveIntegerField check constraint

**URL:** <https://forum.djangoproject.com/t/upgrading-from-old-django-version-2-2-add-positiveintegerfield-check-constraint/35539>\
**Category:** Django Internals\
**Created:** [October 9, 2024, 6:30pm UTC](https://forum.djangoproject.com/t/upgrading-from-old-django-version-2-2-add-positiveintegerfield-check-constraint/35539 "2024-10-09T18:30:19Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![vebp](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/vebp/32/8488_2.png) [@vebp](https://forum.djangoproject.com/u/vebp)\
**Post date:** [October 9, 2024, 6:30pm UTC](https://forum.djangoproject.com/t/upgrading-from-old-django-version-2-2-add-positiveintegerfield-check-constraint/35539/1 "2024-10-09T18:30:19Z")

</div>

Good day, I had to upgrade a django app from a really old version (2.2). Looking at the docs on this [Django 3.0 release notes | Django documentation | Django](https://docs.djangoproject.com/en/5.1/releases/3.0/) I cross with this line  
“On MySQL 8.0.16+, `PositiveIntegerField` and `PositiveSmallIntegerField` now include a check constraint to prevent negative values in the database.”  
One of my models uses a “PositiveIntegerField” . I’ve made some tests locally but it seems that django only add the constraint when the table is created. Is there a consideration to make django add this constraint or is it okey to add it manually using mysql?

Thanks

---

<div class="post-metadata">

**Author:** ![matthiask](https://sea2.discourse-cdn.com/flex026/user_avatar/forum.djangoproject.com/matthiask/32/115_2.png) [@matthiask](https://forum.djangoproject.com/u/matthiask)\
**Post date:** [October 10, 2024, 12:12pm UTC](https://forum.djangoproject.com/t/upgrading-from-old-django-version-2-2-add-positiveintegerfield-check-constraint/35539/2 "2024-10-10T12:12:09Z")

</div>

I didn’t check, but copy-pasting some of the output of the appropriate `manage.py sqlmigrate` command sounds fine to me. Generally you really do not want to apply DDL by hand to the database and do everything through the migration framework, but maybe you’re fine in this case.

You should ensure that the constraint’s name really is the same as the one Django’s migration framework would generate (`sqlmigrate` helps with this).

You could try using `operations.RunSQL` to add the constraint using `CREATE ... IF NOT EXISTS` or something if that functionality exists in MySQL. If you have project-specific test, the test database is recreated by default each time you’re rerunning the test suite; the constraint would already exist in this case, so you’d want to skip the relevant SQL.
