#Model field deletion

5 messages · Page 1 of 1 (latest)

pine plinth
#

I want to delete one field from my model; as per the docs I go ahead with the removal in models.py as well as all references to that field in the code, and then run migrations/migrate, all without errors. But it seems the database part of the deletion does not happen, the column is still there in postgres. And all the pages that rely on that model endup with a field missing error.

ProgrammingError at /ouvrages/165/1
column website_composant.prix_vente does not exist
LINE 1: ...cout_unitaire", "website_composant"."cout_total", "website_c...

For now I ended up adding the field againg, and everything works fine, but I'd like to understand clearly how to get through with field deletion, does anyone have a clue ?

zinc hedge
#

Is this in development, or once deployed?

#

Running makemigrations should add a migration file that defines the field removal, and then running migrate is what actually changes the database—you ran both of those? What was the output from both commands?

pine plinth
#

I went through the process again to confirm all steps, and now it just works very well, I'm quite puzzled 😅 . I post the details just for the record, but the case is solved. I swear I had exactly the same behaviour previously, but I probably missed something ...

  • remove all occurences of field 'prix_vente' in models and anywhere else.

  • run migrations:


ubuntu@vps-xxxxxxx:~$ sudo docker exec -it web /bin/bash
dcrm@xxxxx:~/web$ python manage.py makemigrations
Migrations for 'website':
  website/migrations/0077_remove_composant_prix_vente_alter_circuit_type.py
    - Remove field prix_vente from composant
dcrm@xxxxx:~/web$ python manage.py migrate
Operations to perform:
  Apply all migrations: admin, auth, contenttypes, sessions, website
Running migrations:
  Applying website.0077_remove_composant_prix_vente_alter_circuit_type... OK

The contents of the migration file:


# Generated by Django 5.0.6 on 2025-02-24 14:36

from django.db import migrations, models


class Migration(migrations.Migration):

    dependencies = [
        ('website', '0076_composant_prix_vente_alter_circuit_type_and_more'),
    ]

    operations = [
        migrations.RemoveField(
            model_name='composant',
            name='prix_vente',
        ),
    ]
final snow
#

makemigrations is not something that is meant to be ran live/in a Docker - is is meant to be ran as part of you development process and the migration files comitted into the source code repo. You then run migrate to apply the changes in production/Docker.