#25313: Document how to migrate from a built-in User model to a custom User 
model
-------------------------------+------------------------------------
     Reporter:  Carl Meyer     |                    Owner:  nobody
         Type:  New feature    |                   Status:  new
    Component:  Documentation  |                  Version:  1.8
     Severity:  Normal         |               Resolution:
     Keywords:                 |             Triage Stage:  Accepted
    Has patch:  0              |      Needs documentation:  0
  Needs tests:  0              |  Patch needs improvement:  0
Easy pickings:  0              |                    UI/UX:  0
-------------------------------+------------------------------------

Comment (by Justin Smith):

 Replying to [comment:2 Aymeric Augustin]:
 > I did it at least twice. Unfortunately I don't remember all the details.
 >
 > I think a reasonable procedure is:
 >
 > 1. Create a custom user model identical to `auth.User`, call it `User`
 (so many-to-many tables keep the same name) and set `db_table='auth_user'`
 (so it uses the same table)
 > 2. Throw away all your migrations
 > 3. Recreate a fresh set of migrations
 > 4. Sacrifice a chicken, perhaps two if you're anxious; also make a
 backup of your database
 > 5. Truncate the `django_migrations` table
 > 6. Fake-apply the new set of migrations
 > 7. Unset `db_table`, make other changes to the custom model, generate
 migrations, apply them
 >
 > It is highly recommended to do this on a database that enforces foreign
 key constraints. Don't try this on SQLite on your laptop and expect it to
 work on Postgres on the servers!

 Just recently had to go through this process using a Microsoft SQL Server
 backend and used the steps above as my guideline. Just thought I'd drop in
 and include some of my notes just in case they can help anyone in the
 future.

 Notes (by step):
 1. Make sure the custom user model identical to auth.User is an
 `AbstractUser` model. I originally made this mistake because I did an
 `inspectdb auth_user` and just copy/pasted so I left it as `models.Model`
 at first. Since I copied and pasted from `inspectdb` I went ahead and
 removed `managed = False`
 2. Quick shortcut to delete migrations for all apps in a project I used
 was `find . -path "*/migrations/*.py" -not -name "__init__.py" -delete`.
 3. No additional notes
 4. Not kidding about the back up I had to start over a few times
 5. No additional notes
 6. I did `--fake-initial` first few times and not `--fake`
 7. No notes

 Thank you very much for posting this in the first place I am not sure I
 would have figured this out on my own and saved us mid-project. I have
 learned my lesson about starting a django project and not setting up
 custom user model.

-- 
Ticket URL: <https://code.djangoproject.com/ticket/25313#comment:9>
Django <https://code.djangoproject.com/>
The Web framework for perfectionists with deadlines.

-- 
You received this message because you are subscribed to the Google Groups 
"Django updates" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/064.7fbca9af17ead0ffe55257f807159338%40djangoproject.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to