**Just to be clear, I have read all other articles on and they’re all a billion years ago and Django has made several revisions since these posts were posted. **
So, I had decide to add a new views to my Django model name MediaModule
.
Previously, and as per procedure, you add a new field, give is a default value to it can populate the new field with a value for all existing objects in your Model – which makes sense – and you;re as happy as Larry (however happy Larry may be).
However, today, Django has decided to tell me that, basically, my new field does not exist:
django.db.utils.OperationalError: no such column: media_mediamodule.article_pill
This takes place when I run py manage.py makemigrations
So what did I try?
Well, apart from selling my sanity, I thought it was a migrations issue, so I deleted all migration files from the migrations
folder and deleted everything except for the __init__.py
file.
I commented out the “offending” field, & it has no issue.
I checked the database schema and it returned the following result:
('CREATE TABLE "media_mediamodule" ("uuid" char(32) NOT NULL PRIMARY KEY, "article_headline" varchar(100) NOT NULL, "article_body" text NOT NULL, "article_synopsis" text NULL, "article_journalist" varchar(20) NOT NULL, "article_date" datetime NOT NULL, "article_image" varchar(100) NOT NULL, "article_image_summary" text NULL, "article_identifier" varchar(50) NOT NULL UNIQUE)',)
>>>
But if course it wouldn’t include the new field because it would not allow me to further diagnose the issue until I removed the offending field.
Why is this happening? I have cleared the cache, I have cleared the migration history and initiated a new migration and I have added a default field so existing model objects can have this new field populated. I am doing everything correctly
The use of the model works with no issues but why is Django having some sort of nervous breakdown every time I try and insert a new object model? It’s just silly.
This is my models.py
file
from django.db import models
import uuid, random, string
def generate_unique_id(charlimit):
'''This will generate a random set of numbers and letters which will be used to generate a unique URL for each object in the model.
'''
random_string = ''.join(random.choices(string.ascii_lowercase + string.digits, k=charlimit)) # Generates a random string
return f"{random_string}"
def generate_unique_uuid():
'''This will generate a random set of numbers and letters which are to be derrived from the UUID methodology, which will be used to generate a unique URL for each object in the model.
'''
return str(uuid.uuid4())[:8]
# Create your models here.
class MediaModule(models.Model):
uuid = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
article_identifier = models.CharField(max_length=50, default=generate_unique_id(12), unique=True, editable=False)
article_headline = models.CharField(max_length=100)
article_body = models.TextField()
article_synopsis = models.TextField(null=True)
article_journalist = models.CharField(max_length=20)
article_date = models.DateTimeField(auto_now=True)
article_image = models.ImageField(upload_to='assets')
article_image_summary = models.TextField(default="", null=True, blank=True)
article_pill = models.TextField(default="", null=True, max_length=225) ##this is the field Django has an issue with
def __str__(self):
return self.article_headline
The things I have tried are listed above.