Django transitions

I am trying to create a blog in django. I got to 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 field without a 'post_id' column to posts without a default; we cannot do this (the database needs to be tagged to populate existing rows). Pick a solution:
1) Provide a one-off default (will be set for all existing rows)
2) Close and add the default to models.py

Even if I press 1 and try to set a random nonce it migrates successfully, but later the website crashes "without such a table: blog_posts". But I think this should work without workarounds like manually setting the default.

I tried playing with the primary keys for messages and responses. I tried to remove them completely so that django would automatically install them itself and tried to change it from AutoField to UUIDField and vice versa, but that didn't work. What am I doing wrong?

+3


source to share


1 answer


You are seeing this error message because django is trying to create a consistent migration history and it is complaining that if there was a database that held data with your old migrations and you tried to add an invalid field, it would not know that do.

The migration is supposed to be implemented in a version control system and used in different development / production environments. If you add a field that shouldn't be null, existing data from other environments (for example, a production database that had models that didn't have a field post_id

), then django will warn you about it, with the error message you received. and we offer two solutions:



  • This field should always have a default value (you need to change models.py)

  • This is a one-time migration and you supply a one-time value like "LEGACY" to mark the data before migrating.

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

.

+3


source







All Articles