Changes between Initial Version and Version 2 of Ticket #35410


Ignore:
Timestamp:
Apr 26, 2024, 2:01:28 PM (7 months ago)
Author:
Ebram Shehata
Comment:

Replying to Tim Graham:

You didn't provide the full traceback but I suspect the error comes from Department.objects.get_or_create(). I don't think Django is at fault. You could make the migrations in two steps, adding the default only after the Department table is created.

Oh, I just added the traceback.

  • I think that is not the expected behavior when someone uses a callable as default parameter. I expect it to be used when the value was not provided and I'm trying to create an instance and of course not when I make migrations and that is working as expected when the target model is not a custom user model (inheriting from models.Model).
  • I also think the work around of creating a data migration for creating the default instance is not actually convenient. Because I think the scenario could be:

Develop stuff. Create migration for the project and the a special migration for that default instance. After that we'll have to actually migrate the database so far to create that instance. Now develop the custom user profile and make migrations. Now we should be able to migrate database but no. Here's a problem that appeared with this scenario after adding the custom user model in this case:

Operations to perform:
  Apply all migrations: admin, auth, contenttypes, profiles, sessions
Traceback (most recent call last):
  File "/SOME-DIR/blank_django/src/blanked/manage.py", line 22, in <module>
    main()
  File "/SOME-DIR/blank_django/src/blanked/manage.py", line 18, in main
    execute_from_command_line(sys.argv)
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
    utility.execute()
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
    self.fetch_command(subcommand).run_from_argv(self.argv)
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/base.py", line 413, in run_from_argv
    self.execute(*args, **cmd_options)
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/base.py", line 459, in execute
    output = self.handle(*args, **options)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/base.py", line 107, in wrapper
    res = handle_func(*args, **kwargs)
          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/commands/migrate.py", line 302, in handle
    pre_migrate_apps = pre_migrate_state.apps
                       ^^^^^^^^^^^^^^^^^^^^^^
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/utils/functional.py", line 47, in __get__
    res = instance.__dict__[self.name] = self.func(instance)
                                         ^^^^^^^^^^^^^^^^^^^
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/migrations/state.py", line 566, in apps
    return StateApps(self.real_apps, self.models)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/migrations/state.py", line 637, in __init__
    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field admin.LogEntry.user was declared with a lazy reference to 'profiles.userprofile', but app 'profiles' doesn't provide model 'userprofile'.

Overall about the scenario of having to create a data migration for that default instance, I think it's not convenient and it is not what's expected from a callable in default parameter. I think it should be behaving the same as with regular models, not having a special behavior if we're creating a custom user model.

Legend:

Unmodified
Added
Removed
Modified
  • Ticket #35410

    • Property Resolutioninvalid
    • Property Status newclosed
  • Ticket #35410 – Description

    initial v2  
    6565from `AUTH_USER_MODEL` setting, I can create migrations successfully and migrate the
    6666database too! I also could create instances that have the default department as expected.
     67
     68
     69Here's the full traceback:
     70
     71{{{
     72Traceback (most recent call last):
     73  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute
     74    return self.cursor.execute(sql, params)
     75           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     76  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute
     77    return super().execute(query, params)
     78           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     79sqlite3.OperationalError: no such table: profiles_department
     80
     81The above exception was the direct cause of the following exception:
     82
     83Traceback (most recent call last):
     84  File "/SOME-DIR/blank_django/src/blanked/manage.py", line 22, in <module>
     85    main()
     86  File "/SOME-DIR/blank_django/src/blanked/manage.py", line 18, in main
     87    execute_from_command_line(sys.argv)
     88  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 442, in execute_from_command_line
     89    utility.execute()
     90  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/__init__.py", line 436, in execute
     91    self.fetch_command(subcommand).run_from_argv(self.argv)
     92  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/base.py", line 413, in run_from_argv
     93    self.execute(*args, **cmd_options)
     94  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/base.py", line 454, in execute
     95    self.check()
     96  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/management/base.py", line 486, in check
     97    all_issues = checks.run_checks(
     98                 ^^^^^^^^^^^^^^^^^^
     99  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/core/checks/registry.py", line 88, in run_checks
     100    new_errors = check(app_configs=app_configs, databases=databases)
     101                 ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     102  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/contrib/auth/checks.py", line 84, in check_user_model
     103    if isinstance(cls().is_anonymous, MethodType):
     104                  ^^^^^
     105  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/base.py", line 535, in __init__
     106    val = field.get_default()
     107          ^^^^^^^^^^^^^^^^^^^
     108  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/fields/related.py", line 1134, in get_default
     109    field_default = super().get_default()
     110                    ^^^^^^^^^^^^^^^^^^^^^
     111  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/fields/__init__.py", line 1021, in get_default
     112    return self._get_default()
     113           ^^^^^^^^^^^^^^^^^^^
     114  File "/SOME-DIR/blank_django/src/blanked/profiles/models.py", line 11, in unassigned_department
     115    return Department.objects.get_or_create(name="UNASSIGNED")[0].pk
     116           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     117  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/manager.py", line 87, in manager_method
     118    return getattr(self.get_queryset(), name)(*args, **kwargs)
     119           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     120  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/query.py", line 948, in get_or_create
     121    return self.get(**kwargs), False
     122           ^^^^^^^^^^^^^^^^^^
     123  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/query.py", line 645, in get
     124    num = len(clone)
     125          ^^^^^^^^^^
     126  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/query.py", line 382, in __len__
     127    self._fetch_all()
     128  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/query.py", line 1928, in _fetch_all
     129    self._result_cache = list(self._iterable_class(self))
     130                         ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     131  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/query.py", line 91, in __iter__
     132    results = compiler.execute_sql(
     133              ^^^^^^^^^^^^^^^^^^^^^
     134  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/models/sql/compiler.py", line 1562, in execute_sql
     135    cursor.execute(sql, params)
     136  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 122, in execute
     137    return super().execute(sql, params)
     138           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     139  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 79, in execute
     140    return self._execute_with_wrappers(
     141           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     142  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 92, in _execute_with_wrappers
     143    return executor(sql, params, many, context)
     144           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     145  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 100, in _execute
     146    with self.db.wrap_database_errors:
     147  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/utils.py", line 91, in __exit__
     148    raise dj_exc_value.with_traceback(traceback) from exc_value
     149  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/utils.py", line 105, in _execute
     150    return self.cursor.execute(sql, params)
     151           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     152  File "/SOME-DIR/blank_django/src/venv/lib/python3.11/site-packages/django/db/backends/sqlite3/base.py", line 329, in execute
     153    return super().execute(query, params)
     154           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
     155django.db.utils.OperationalError: no such table: profiles_department
     156
     157}}}
Back to Top