upgrading from old Django version 2.2. add PositiveIntegerField check constraint

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 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

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.

1 Like