Migrations issue

I have a custom user model and so I made changes to the custom user model and now I want to migrate those changes to the database. When I run makemigrations I get the following error and I would really like to understand how this works and what determines the order at what the migrations are applied. Knox is a third party app to use with token authentication. I have truncated the database and have done many other things people have said I should do but to no avail the same error appears. How am I then suppose to update the user model if this happens?

The error I am getting is:
Migration knox.0001_initial is applied before its dependency users.0001_initial on database ‘default’.

The Migrations docs do a great job of explaining all the details.

As far as the “sequence of events” is concerned, migrations are created with a list of dependencies defined in the migration file itself. That identifies what migrations this migration needs to follow.

If you’ve created your custom user model after doing your initial migration, then the easiest thing to do is to drop your database and recreate it. Or, if you’ve manually deleted any tables, you’re likely to have problems there, too.
(One of the other features of migrations is that it tracks which migrations have already been applied, and doesn’t try to apply them again. However, if you’ve made any manual changes to the database, then migrations doesn’t know about it and errors are likely to occur.)

Also going to throw out a very strong suggestion that if you’re creating a custom user model that you don’t call it “user”. Do yourself a favor and use a different name.

But, in the absence of wanting to spend an unbounded amount of time trying to fix a problem, if I were in this situation, I’d drop the database, recreate it, delete all of my migration files, run makemigrations and migrate to get my initial set going, and move forward from there.

2 Likes

Hi Ken

Ok thanks for the information and I will go read the docs. I am going to keep creating new things so every time I add an app and create models I have to drop the whole database, or if I add fields to a model I have to drop the whole database because the custom user model is going to change over time so one day when I have 100 or 10 or a million users if I add a field to the user model I have to make a backup of the database and then drop it and then load the database again? Seems a bit unproductive having to drop the entire db every time I add a new app or make changes to a model. What if one day I have a million users and I need to complete change the user model?

No - migrations will take care of all that, assuming -

  • You started out with a working environment
    • Which also means, if you’re going to use a custom user model, that you have defined that model before you perform your first makemigrations / migrate
  • You never touch the database schema outside of using migrations
  • You never empty any of the core django tables

Now, none of those issues are irreparable, just time consuming to identify the root issue and fix them.

I’m only suggesting the dropping and recreating the database because at this time, it’s your most time-effective solution.

Ken

Hi Ken ok thanks I learn something every day and thanks for all your input