Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Django Lazy Reference ValueError after upgrading to the latest version and running migrations, #1120

Open
smit-mehta25 opened this issue Feb 18, 2022 · 7 comments
Labels
Milestone

Comments

@smit-mehta25
Copy link

smit-mehta25 commented Feb 18, 2022

    raise ValueError("\n".join(error.msg for error in errors))
ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'oauth.clientapplication', but app 'oauth' isn't installed.
The field oauth2_provider.AccessToken.source_refresh_token was declared with a lazy reference to 'oauth.clientrefreshtoken', but app 'oauth' isn't installed.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'oauth.clientapplication', but app 'oauth' isn't installed.
The field oauth2_provider.RefreshToken.access_token was declared with a lazy reference to 'oauth.clientaccesstoken', but app 'oauth' isn't installed.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'oauth.clientapplication', but app 'oauth' isn't installed.

I am constantly facing this issues. Is there any solutions for this one,

oauth2_provider setting configurations:

OAUTH2_PROVIDER_APPLICATION_MODEL = "oauth.ClientApplication"
OAUTH2_PROVIDER_ACCESS_TOKEN_MODEL = "oauth.ClientAccessToken"
OAUTH2_PROVIDER_GRANT_MODEL = "oauth.ClientGrant"
OAUTH2_PROVIDER_REFRESH_TOKEN_MODEL = "oauth.ClientRefreshToken"
OAUTH2_PROVIDER_ID_TOKEN_MODEL = "oauth.ClientIdToken"
OAUTH2_PROVIDER = {
    "ACCESS_TOKEN_EXPIRE_SECONDS": 1800,
    "SCOPES": {
        "uid": "User ID read access",
    },
}

Even, I tried to apply run_before to my custom initial migrations. But No luck.

run_before = [
        ('oauth2_provider', '0001_initial'),
    ]

This is with Django==3.2.11 and django-oauth-toolkit==1.7.0

Below is the order of Apply migrations.

  Applying oauth.0001_initial_squashed_0004_auto_20220218_1009...accounts_ui client does not exist
accounts_ui client created
 OK
  Applying oauth2_provider.0001_initial... OK
  Applying oauth2_provider.0002_auto_20190406_1805... OK
  Applying oauth2_provider.0003_auto_20201211_1314... OK
  Applying oauth2_provider.0004_auto_20200902_2022... OK
  Applying oauth2_provider.0005_auto_20211222_2352... OK

But still i am facing the above error. I have tried everything that i could from other open issues. Such as ... Swappable models. Clean migrations, run before.

@smit-mehta25
Copy link
Author

#634

@n2ygk
Copy link
Member

n2ygk commented Mar 21, 2022

@smit-mehta25 I assume your comment means this is a dup of #634?

@undo-nothing
Copy link

@smit-mehta25 same problem, i update to django 4.0.2 solved.

@phith0n
Copy link
Contributor

phith0n commented Jun 24, 2022

similar issue when I run migration: ./manage.py migrate xxxx zero

my django version is 4.0.5:

ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
The field oauth2_provider.IDToken.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'ucenter.application', but app 'ucenter' doesn't provide model 'application'.

@n2ygk
Copy link
Member

n2ygk commented Jun 25, 2022

@phith0n Can you be specific about what versions you are using? The issue was created back several months ago and also didn't reference the version. Please fill out this template:

Describe the bug

To Reproduce

Expected behavior

Version

  • I have tested with the latest published release and it's still a problem.
  • I have tested with the master branch and it's still a problem.

Additional context

Also, I would ask that you repeat your reproducing test with Django 3.2 and with DOT 1.7.1 to help isolate if this is due to a change in Django or in DOT 2.x.

Thanks.

@phith0n
Copy link
Contributor

phith0n commented Jun 25, 2022

Hi @n2ygk , I think I may have found the problem.

Refer to the document https://django-oauth-toolkit.readthedocs.io/en/latest/advanced_topics.html#AbstractApplication, everything is OK after I added run_before = [('oauth2_provider', '0001_initial'),] to the migrations that created ucenter.Application.

However, the error models.E022 stated in the documentation was not thrown.

@n2ygk
Copy link
Member

n2ygk commented Jun 26, 2022

I believe this run_before requirement is a symptom of the migrations not listing all the required dependencies on swappable models. In the initial migration, I suspect this in 0001_initial:

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL)
    ]

should be changed to this to make sure all possible swappable models become dependencies:

    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        migrations.swappable_dependency(settings.APPLICATION_MODEL),
        migrations.swappable_dependency(settings.ACCESS_TOKEN_MODEL),
        migrations.swappable_dependency(settings.ID_TOKEN_MODEL),
        migrations.swappable_dependency(settings.GRANT_MODEL),
        migrations.swappable_dependency(settings.REFRESH_TOKEN_MODEL),
    ]

This has to do with auto-generated migrations (manage.py makemigrations) assuming the only possible swappable model is the User model as that's the "usual" case in other parts of Django.

I haven't had time to test this. Swappable models have been an ongoing point of confusion with this library forever.

See also #871, #778, #634 and https://github.com/openwisp/django-swappable-models (which I don't quite understand the point of).

@n2ygk n2ygk added this to the Future milestone Jun 26, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

4 participants