manabreak
manabreak

Reputation: 5597

How to set the default value for existing entries when adding a new column?

I have a model in my Django app that requires a new BooleanFieldcolumn to be added. This column should never be null - instead, I'd like all the existing entries to set this field to False.

How do I do that? If I just add the new field and set default = False, it'll cause an OperationalError: no such column error.

Upvotes: 0

Views: 5947

Answers (4)

manabreak
manabreak

Reputation: 5597

It turns out I had missed that the migrate command requires the app name. So, I had to run the command like this:

manage.py makemigrations app_name

Upvotes: 0

wobbily_col
wobbily_col

Reputation: 11891

Sounds like you need to migrate your database after adding the new column. Its like when you use the syncdb command to do the "create table" commands when first creating your database, the migrations will write "alter table" changes to your database.

https://docs.djangoproject.com/en/1.8/topics/migrations/

Upvotes: 0

Ajai
Ajai

Reputation: 901

By default BooleanField column is False, you don't need to specify anything. Just run python manage.py migrate. It should work.

Upvotes: -1

Dominic Rodger
Dominic Rodger

Reputation: 99771

You need to use Django's migrations, which will allow you to add the field to the database.

In short, you make the change to your model, create a migration with manage.py makemigrations, and then run it with manage.py migrate.

Upvotes: 2

Related Questions