#33366: Foreign key to settings.AUTH_USER_MODEL causes hard-coded alteration in
migration
-------------------------------------+-------------------------------------
     Reporter:  Andrew Chen Wang     |                    Owner:  nobody
         Type:  Bug                  |                   Status:  new
    Component:  Database layer       |                  Version:  4.0
  (models, ORM)                      |
     Severity:  Normal               |               Resolution:
     Keywords:                       |             Triage Stage:
                                     |  Unreviewed
    Has patch:  0                    |      Needs documentation:  0
  Needs tests:  0                    |  Patch needs improvement:  0
Easy pickings:  0                    |                    UI/UX:  0
-------------------------------------+-------------------------------------
Changes (by Keryn Knight):

 * cc: Keryn Knight (added)
 * status:  closed => new
 * resolution:  invalid =>


Comment:

 I'm going to risk the wrath of the fellows and re-open to get it double-
 checked, because I ''think'' it's reproducible:

 {{{
 $ git checkout 3.2.10
 $ django-admin startproject test33366
 $ cd test33366
 $ django-admin startapp testmodels
 $ vim testmodels/models.py
 }}}
 put the following contents in and save:
 {{{
 class MyModel(models.Model):
     user = models.ForeignKey(
         settings.AUTH_USER_MODEL, on_delete=models.CASCADE,
 related_name="%(app_label)s_%(class)s"
     )
 }}}
 add `testmodels` to the `INSTALLED_APPS` and then resume:
 {{{
 $ python manage.py makemigrations testmodels
 Migrations for 'testmodels':
   testmodels/migrations/0001_initial.py
     - Create model MyModel
 $ python manage.py makemigrations testmodels
 No changes detected in app 'testmodels'
 }}}
 looking OK so far, here's the migration:
 {{{
 dependencies = [
     migrations.swappable_dependency(settings.AUTH_USER_MODEL),
 ]

 operations = [
     migrations.CreateModel(
         name='MyModel',
         fields=[
             ('id', models.BigAutoField(auto_created=True,
 primary_key=True, serialize=False, verbose_name='ID')),
             ('user',
 models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
 related_name='testmodels_mymodel', to=settings.AUTH_USER_MODEL)),
         ],
     ),
 ]
 }}}

 Now moving to 4.0:
 {{{
 $ git checkout 4.0
 Previous HEAD position was 0153a63a67 [3.2.x] Bumped version for 3.2.10
 release.
 HEAD is now at 67d0c4644a [4.0.x] Bumped version for 4.0 release.
 $ python manage.py makemigrations testmodels
 /path/to/django/django/conf/__init__.py:222: RemovedInDjango50Warning: The
 USE_L10N setting is deprecated. Starting with Django 5.0, localized
 formatting of data will always be enabled. For example Django will display
 numbers and dates using the format of the current locale.
   warnings.warn(USE_L10N_DEPRECATED_MSG, RemovedInDjango50Warning)
 Migrations for 'testmodels':
   testmodels/migrations/0002_alter_mymodel_user.py
     - Alter field user on mymodel
 }}}

 and the second migration is now:
 {{{
 dependencies = [
     ('auth', '0012_alter_user_first_name_max_length'),
     ('testmodels', '0001_initial'),
 ]

 operations = [
     migrations.AlterField(
         model_name='mymodel',
         name='user',
 field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE,
 related_name='%(app_label)s_%(class)s', to='auth.user'),
     ),
 ]
 }}}

 Note that it's no longer a swappable dependency, nor is it cased the same
 as as the global default (`'auth.User'` vs `'auth.user'`)...

-- 
Ticket URL: <https://code.djangoproject.com/ticket/33366#comment:5>
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 view this discussion on the web visit 
https://groups.google.com/d/msgid/django-updates/074.cb4e8597aead82ee5ce87a52d6dbb5e4%40djangoproject.com.

Reply via email to