Makemigration error on SlugField in Thing data model


#1

Problem in “Setting Up Your Model” section of the book.

When I ran the makemigration command on the Thing data model, I got the following error related to the SlugField definition (slug = models.SlugField( unique = True))

You are trying to add a non-nullable field ‘book_slug’ to addressbook 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)
  2. Quit, and let me add a default in models.py
    Select an option: 2

Django wants the model to specify a default value. But a default value is not unique! How do I get around this error? I’m running Django 1.7.


#2

Interesting! Looks like you’re adding a slug to your model after your initial migration. How many rows/objects do you have in your database so far? Might be worth removing “unique=True”, doing the migration, then adding unique, and then doing the migration again. That (I am 75% sure) would work correctly. The reason I ask about # of objects in the database is in case it doesn’t work, in case it might be worth it to wipe the database and start over. :)


#3

Thanks for your response. I deleted all migrations files and re-executed the makemigrations command. It completed without error. I did so without removing the unique=yes attribute from the definition of the SlugField.


#4

Yay! Glad I was able to help.