I am trying to build a blog on django. I have gone as far as creating models. Here they are:
from django.db import models import uuid class Users(models.Model): username = models.CharField(max_length = 32, primary_key = True) password = models.CharField(max_length = 32) email = models.EmailField() registration_date = models.DateTimeField(auto_now_add = True) class Posts(models.Model): author = models.ForeignKey("Users") header = models.CharField(max_length=100) body = models.TextField() pub_date = models.DateTimeField(auto_now_add = True) mod_date = models.DateTimeField(auto_now = True) upvotes = models.PositiveIntegerField() views = models.PositiveIntegerField() post_id = models.AutoField(primary_key = True) class Answers(models.Model): body = models.TextField() author = models.ForeignKey("Users") pub_date = models.DateTimeField(auto_now_add = True) mod_date = models.DateTimeField(auto_now = True) post = models.ForeignKey("Posts") answer_id = models.UUIDField(primary_key = True, default=uuid.uuid4)
python manage.py migrate I get the following:
You are trying to add a non-nullable field 'post_id' to posts without a default; we can't do that (the database need mething to populate existing rows). Please select a fix:
1) Provide a one-off default now (will be set on all existing rows)
2) Quit, and let me add a default in models.py
Even if I press 1 and try to set a random one-off value, it migrates successfully but later on the website crashes with "no such table: blog_posts". But I think it should work without such workarounds as setting the default value manually anyway.
I tried playing with primary keys for Posts and Answers. I tried completely removing them so that django automatically sets them itself and tried changing it from AutoField to UUIDField and vice versa but it didn't help. What am I doing wrong?