How to flush/clear database for single app, i.e. NOT the Project?

1,258 views
Skip to first unread message

JoeCodeswell

unread,
May 16, 2016, 5:19:36 PM5/16/16
to Django users
How can I flush/clear database for single app, i.e. NOT the Project?

I have a multi-app project.
In a particular app I created my model with single class.
I populated the db for that model with the Admin Interface.
Now i have changed the model to have 2 classes related by a ForeignKey.
I ran makemigrations on that app and it said, "you are trying to add a non-nullable field 'song' to part without a default; we can't do that (the database needs something to populate existing rows)."
I don't want to keep the existing populated tables for that app.
How can i start over for that app?
I can see this happening again and again for me.

Thanks in advance.
Love and peace,
Joe

Erik Cederstrand

unread,
May 16, 2016, 6:32:59 PM5/16/16
to Django Users

> Den 16. maj 2016 kl. 19.16 skrev JoeCodeswell <joecod...@gmail.com>:
>
> How can I flush/clear database for single app, i.e. NOT the Project?
>
> I have a multi-app project.
> In a particular app I created my model with single class.
> I populated the db for that model with the Admin Interface.
> Now i have changed the model to have 2 classes related by a ForeignKey.
> I ran makemigrations on that app and it said, "you are trying to add a non-nullable field 'song' to part without a default; we can't do that (the database needs something to populate existing rows)."
> I don't want to keep the existing populated tables for that app.

I can do it manually before the migration, i.e.:

YourModel.objects.all().delete()

If you want to do it as part of the migration, then create a data migration (https://docs.djangoproject.com/en/1.9/topics/migrations/#data-migrations) that deletes all relevant model instances, and place the migration before the migration that adds the ForeignKey.

Erik

James Schneider

unread,
May 17, 2016, 8:02:43 AM5/17/16
to django...@googlegroups.com
Erik's answer is correct because it answers the question you specifically asked. You would need to delete all instances (rows) for any models that are part of your app, either manually or as part of a data migration. Keep in mind that if your models interact with other apps (ie, foreign keys pointing at Group or User from django.contrib), you may be left with orphaned and/or incomplete data if your app FK's do not use on_delete=models.CASCADE (which is the current default, but will not be in upcoming Django releases [1]). Probably won't be an issue, but is something to keep in mind.

I'm guessing that you are still in the initial development stages of your app. Your actual question is likely "how do I keep Django from pestering me about non-nullable fields while I'm doing my development updates?" The best answer is "you can't without a bit of work for each new field". Every time you add a new non-nullable field with no default to a subsequent migration, you'll get this question. Even if you wipe out all of the instances as Erik pointed out, you'll still get asked this question because the migration process does not check whether or not a table contains existing data prior to asking for a default value for your new non-nullable field, intentionally [2]. The only way to avoid it would be to set a default= for the field (either a static value or a callable).

You should also have a look here [3]. Django provides some guidance on dealing with non-nullable fields, with extra help for those fields that also have unique=True (where a single static default value will not work).

Another option would be a customized set of migrations. An example can be found here [4].

Yet another option (since you don't care to keep the data), would be to try a 'zero' migration specific to your app first, which will remove (reverse?) all migrations related to that app (manage.py migrate myapp zero). [5] After that, you can probably run your app migration normally. I'm guessing that all of your migrations would need to be reversible for this to work, which is likely if you haven't customized the migrations for your app. I've never tried it myself. Keep a copy of the DB in the event things go sideways if you care to keep your other data intact.


HTH,

-James

JoeCodeswell

unread,
May 17, 2016, 4:28:30 PM5/17/16
to Django users
Dear Erik and James,

Thanks for the insightful, deep answers to my questions.

This is invaluable information.

Thanks again to both of you.

Love and peace,

Joe
Reply all
Reply to author
Forward
0 new messages