Search code examples
pythondjangosignals

You are trying to add a non-nullable field 'post' to stream without a default; we can't do that


I have models.py

  class Stream(models.Model):

        following = models.ForeignKey(User, on_delete=models.CASCADE, 
        related_name='stream_following')
        user = models.ForeignKey(User, on_delete=models.CASCADE)
        post = models.ForeignKey(Post, on_delete=models.CASCADE)
        date = models.DateTimeField()

and function

  def add_post(sender, instance, *args, **kwargs):

    post = instance 
    user = post.user 
    followers = Follow.objects.all().filter(following=user)
    for follower in followers:
        stream = Stream(post=post, user=follower.follower, date=post.posted, following=user)
        stream.save()

when I'm trying command py manage.py makemigrations I have an issue.

You are trying to add a non-nullable field 'post' to stream without a default; we can't do that (the database needs something to populate existing rows). Please select a fix:

  1. Provide a one-off default now (will be set on all existing rows with a null value for this column)
  2. Quit, and let me add a default in models.py Select an option:

How to solve that? I put on default smth. However in function add_post I have added date=post.posted Thanks!


Solution

  • you probably changed your models while it already have data in it

    you could choose either:

    a. delete data from that model and re-do migrations,or

    b. delete migrations and sqlite files and re-do migrations,or

    c. choose "Provide a one-off default now " by typing 1 and just add a random existing id number(but it could mess with your data),or

    d. add blank=True,null=True to all fields in your models and re-do migrations,