[Python] Django won't apply null=True changes on fields when running makemigrations and migrate

Discussão em 'Python' iniciado por Stack, Julho 22, 2025.

  1. Stack

    Stack Membro Participativo

    I’m working on a Django project and I’m facing an issue: I modified several fields in one of my models to be null=True, but after running makemigrations and migrate, the changes are not reflected in the database.

    I have a model named Sellers with many fields. For example:

    class Sellers(models.Model):
    ...
    selling_name = models.CharField(max_length=100, null=True, blank=True)
    zip_code = models.IntegerField(null=True, blank=True)
    ...


    However, in the database schema, these fields are still marked as NOT NULL.

    What I’ve tried: I created an empty migration manually:

    python manage.py makemigrations --empty sellersapp -n fix_nullable_fields


    Then manually added AlterField operations like:

    migrations.AlterField(
    model_name='sellers',
    name='selling_name',
    field=models.CharField(max_length=100, null=True, blank=True),


    ),

    After running migrate, it said the migration was applied — but still no actual effect on the DB schema.

    How can I force Django to apply null=True changes to existing fields in the database? Is there a proper way to generate migrations that actually produce the corresponding ALTER TABLE statements? I’m aware that this issue likely stems from a desynchronization between the database schema and Django’s migration state. However, this is the only inconsistency I need to fix, and I believe this is the cleanest approach to do it without resetting everything.

    If there’s a better or safer method to sync the field definitions (null=True) with the actual database schema without losing data, I’d really appreciate any suggestions.

    Continue reading...

Compartilhe esta Página