Ajay Chaurasia
Ajay Chaurasia

Reputation: 119

Migration does not create column in database

makemigrations and migrate are working fine with no error. but when i check database it does not created

heres the model:

class Services(models.Model):
    service_id = models.AutoField(primary_key=True)
    parent_id = models.ForeignKey('self', on_delete=models.SET_NULL, null=True, blank=True,related_name='sub_service')
    service_name = models.CharField(max_length=100)
    service_icon = models.CharField(max_length=500, null=True, blank=True)
    service_image = models.CharField(max_length=500, null=True, blank=True)
    category_id = models.ForeignKey(Category,on_delete=models.CASCADE)
    active_status = models.BooleanField(default=True)
    type = models.SmallIntegerField(blank=True, null=True)
    service_description = models.TextField( null=True, blank=True)
    duration = models.CharField(max_length=100,null=True,blank=True)

I have added duration field later and its not giving me any error while running api

django.db.utils.ProgrammingError: column service_list_services.duration does not exist
LINE 1: ...", "service_list_services"."service_description", "service_l...
 
                                                        
  1. i have tried deleting migration files and then migrating but still.. its not giving error while migrating but doesnot create row.

  2. tried this python manage.py migrate --fake still same

Upvotes: 1

Views: 2190

Answers (1)

Dos
Dos

Reputation: 2507

Basing on your situation (Deleting + recreating all migrations may cause other issues such as circular dependencies depending on how models are constructed), I see two scenarios:

  1. Development scenario (you can drop the db tables)

Restart with a clean persistence: drop the DB tables, delete migration files, run again makemigrations and migrate commands and commit the new migrations file

  1. Production scenario (you cannot lose data)

Restore the deleted migration file, fix inconsistencies in the DB or in the file manually if you changed something by error, when you have code and db in sync do the changes again and make sure su use migrations every time you need to change the persistence. , using the makemigrations and migrate commands.

If the code and the DB are in sync, you can use the squashmigrations command

In general: Avoid to delete a previous migration file, as it may cause dependency issues with other migrations. From Django doc:

You are encouraged to make migrations freely and not worry about how many you have; the migration code is optimized to deal with hundreds at a time without much slowdown. However, eventually you will want to move back from having several hundred migrations to just a few, and that’s where squashing comes in.

Upvotes: 1

Related Questions