Django migrations
Question: 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)
    

After running `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?


Answer: You're seeing this error message because django is trying to build a
consistent history of migrations, and it complains that if there was a
database that held data with your old migrations, and you'd try to add a non-
nullable field, it wouldn't know what to do.

Migrations are supposed to be put into version control and used across
different development/production environments. If you add a field that must
not be null, existing data of other environments (for example a production
database that held models that did not have the field `post_id`) then django
will warn you about this, with the error message that you got, and offer two
solutions:

  1. This field should always be prepoulated with a default value( you have to modify the models.py)

  2. This is a one time migration and you supply a one-off value, for example "LEGACY" to mark pre-migration data.

If you're not in production and there is no valuable data on your development
server, an easy way to fix this error message is just to delete the existing
migration files and run `python manage.py makemigrations && python manage.py
migrate` again.

