Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

Python Django Basics Django Templates Step by Step

On adding step, getting the error you are trying to add a non mutable field 'course'. How to fix this

Error Received: You are trying to add a non-nullable field 'course' to step without a de fault; we can't do that (the database needs something to populate existi ng 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
Select an option:

Here is my code in models.py:

from django.db import models

# Create your models here.
class Course(models.Model):
    created_at = models.DateTimeField(auto_now_add=True)
    title = models.CharField(max_length=255)
    description = models.TextField()

    def __str__(self):
        return self.title

class Step(models.Model):
    title = models.CharField(max_length=255)
    description = models.TextField()
    order = models.IntegerField(default=0)
    course = models.ForeignKey(Course)

    def __str__(self):
        return self.title

1 Answer

Parth Sehgal
Parth Sehgal
362 Points

This is happening because you added a new field to a model and asked Django to do a migration. When Django tried to apply the migrations it realizes that there are some existing objects in the database that don't have this new field associated with it. So here, Django is asking you to either provide a default value in your code or provide a one-off default (that you can specify in the command line). When I run into this I usually provide a one-off default in the command line. Another thing you can do is delete your sqlite database file and apply the migrations to a fresh database without any objects in it.