Search code examples
pythondjangomigration

django sqlmigrate stuck in a buggy migration


I have a small model for restaurants where I added an item_image attribute that receives url string. But instead of CharField I defined it as IntegerField . Then I ran python manage.py makemigrations food and python manage.py sqlmigrate food 0002 . I gave an error

ValueError: Field 'item_image' expected a number but got 'https://cdn-icons-png.flaticon.com/512/1377/1377194.png'.

I noticed the typo and fixed it( changed IntegerField to CharField).

from django.db import models

# Create your models here.
class Item(models.Model):
    
    def __str__(self) -> str:
        return self.item_name
    
    
    item_name = models.CharField(max_length=200)
    item_desc = models.CharField(max_length=200)
    item_price = models.IntegerField()
    item_image = models.CharField(max_length=500,
                                  default='https://cdn-icons-png.flaticon.com/512/1377/1377194.png')

but after I ran

python manage.py sqlmigrate food 0002

the error is still there and when I change it to

python manage.py sqlmigrate food 0003

and then run it. it gives no error but now for changes to take place I have to run

python manage.py migrate

which still gives the error on migration no.0002

How can I ignore this migration?


Solution

  • sqlmigrate [Django-doc] does not migrate, sqlmigrate only prints the (SQL) query it will run when you eventually migrate. This is thus more a debugging tool to see why the query does not work.

    Since you did not (yet) run the 0002 migration on any database, since it would result in an error. You can just remove the migration (together with the 0003), then run makemigrations again to create a new 0002 migration, and migrate.