#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 mikob): Thank you Aymeric et. al for these steps, they worked for me. I made one silly mistake in 1. not creating the user properly. For other people who might also get stuck on this, here is what your User model should look like for step 1: {{{ class User(AbstractUser): class Meta: db_table = 'auth_user' }}} Replying to [comment:12 Dustin Torres]: > 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! > > Thanks for this, I followed your steps and Justin Smith's notes and want to add one more: > > 6b. Any model that has a link to ContentType may be linked to the now stale auth.User content type. I went through and updated all models that were pointing to auth.User content type to my new User model. You can find out what type of objects would be removed due to cascading delete of the stale auth.User content type by running `manage.py remove_stale_contenttypes` **and make sure to answer NO** when asked if you want to proceed. > > If you don't unset `db_table` the above step may be optional since the content type lookup will use auth.User model which will still have a valid database table (although I don't recommend relying on this). -- Ticket URL: <https://code.djangoproject.com/ticket/25313#comment:13> 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 django-updates+unsubscr...@googlegroups.com. To post to this group, send email to django-updates@googlegroups.com. To view this discussion on the web visit https://groups.google.com/d/msgid/django-updates/064.218cdc44dbde0246dc820584e7f30089%40djangoproject.com. For more options, visit https://groups.google.com/d/optout.