Django ignores migration's changes

Hello! I am trying to apply a migration to my Django project for a model. This model previously had no restrictions on its title, but now I want to regexValidate it to lowercase letters and numbers. I made a migration for it and applied the migration, and Django said it worked no problem. Additionally, new indexes were made in the DB, which seemed promising. Upon making a new instance of this Object, however, there was no error in the text field that I did not pass the lowercase and numbers only, and it let me make a title with spaces and uppercase letters.
If I directly add the code into the model.py, then I get the regexValidator to work, and it will not let me make objects with titles involving other characters. Removing the code again stops the validation from working, even if I un and re-apply the migration.

Any insight on why this is happening / how I could fix it?

Welcome @Mobmaker55 !

We’re going to need more detailed information about the code that isn’t working. Please post the actual code you’ve been trying - both versions, along with the sample data that you tried.

import django.core.validators
from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('project', '0001_initial'),
    ]


operations = [
migrations.AlterField(
            model_name='project',
            name='title',
            field=models.CharField(max_length=255, validators=[django.core.validators.MaxLengthValidator(10, 'The project name cannot exceed 10 characters.'), django.core.validators.RegexValidator('^[a-z0-9-]*$', "The project name can only contain lowercase characters, numbers or the dash '-' symbol.")]),
        ),
]

The code that I put in model.py: (this works)

 title = models.CharField(max_length=255,
        validators=[
            MaxLengthValidator(10, "The project name cannot exceed 10 characters."),
            RegexValidator("^[a-z0-9-]*$", "The project name can only contain lowercase characters, numbers or the dash '-' symbol." )])

What did your model look like for the version that didn’t work? Did you define a validator in the model, or did you only create the migration? If the latter, then no, that’s not going to do anything for you because these validators exist and run in Django, not in the database.

If you’re looking to have this validation executing in the database, you need to create a Database Constraint, not a Validator.

Oh okay! I thought the point of migrations was that you could “migrate” a datatype with changes, but without needing to edit the source code of the model – a sort of modular object editing system.
So I need to put validators into the models.py folder, and then we need to start supporting our own fork of the software, then. Thank you!

You can - but in terms of changing the database, not Django. The purpose of the migrate command is to evaluate the defined changes in the migration files and create a set of SQL statements to modify the database. It does not make any changes to your Django code.