Django migration error on changing FK field to regular field
Part of my model looked like this, initially.
class RealTimeLocation(models.Model):
name = models.CharField(max_length=80)
latlng = models.PointField(default=None)
class CabLog(models.Model):
location = models.ForeignKey(RealTimeLocation,
on_delete=models.CASCADE,
related_name='cablog_locations')
Then i thought, no need for an FK field, so i changed the fk field to a Point field.
class RealTimeLocation(models.Model):
name = models.CharField(max_length=80)
latlng = models.PointField(default=None)
class CabLog(models.Model):
location = models.PointField()//
'python manage.py makemigrations ' no issues, but on migrate i'm getting error.
.django.db.utils.ProgrammingError: column "location_id" of relation "myapp_cablog" does not exist.
I tried adding a null=True option to pointfield, deleting the migrate-scripts and redo it many times, no avail. SO before i completely fake-migrate the entire project and startover, do anybody have any solutions for this. I already got some data in DB, so i really don't want to do that. Thanks.
You got this error because you run only makemigrations and migrate.
You must run sqlmigrate also.
Try these three commands:
python manage.py makemigrations appname
python manage.py sqlmigrate appname 0001 #You didn't run this command after makemigrations that's why you got that error
python manage.py migrate
Note: 0001 this value will generate after makemigrations. It can be either 0001, 0002 and so on.