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

Migration dependency issue if extending AbstractApplication #778

Open
anveshagarwal opened this issue Jan 21, 2020 · 6 comments
Open

Migration dependency issue if extending AbstractApplication #778

anveshagarwal opened this issue Jan 21, 2020 · 6 comments

Comments

@anveshagarwal
Copy link
Contributor

If I extend AbstractApplication from oauth2_provider to make a custom application_model say
custom.Application , then the migrations of this custom model should run prior too the oauth2_provider.migrations.0001_inital.py as this migration uses the fields from the custom models.
But after #531 merge, this dependency is removed, so while running migrations, custom Model's migrations are not running before the library's internal migration giving error like

   raise ValueError('Related model %r cannot be resolved' % self.remote_field.model)
   ValueError: Related model 'custom.Application' cannot be resolved

@Sharpek
Copy link

Sharpek commented Feb 18, 2020

Same here ;(

@RenanMarcell
Copy link

RenanMarcell commented Apr 22, 2020

Since the new release 1.1.0 is impossible to extend AbstractApplication from the models to create a customized one without having migration problems. The problem persists even to the newest version. Does anyone have the same problem and how are they solving this issue?

@wojciak
Copy link

wojciak commented May 7, 2020

+1

@Sharpek
Copy link

Sharpek commented May 7, 2020

Hello!

Today I did workaround, it's simply.

  1. To django settings add this:
MIGRATION_MODULES = {
    'oauth2_provider': 'oauth2_lib.migrations'
}
  1. Create package oauth2_lib.migrations
  2. Copy migrations from cp ~/venv36/lib/python3.6/site-packages/oauth2_provider/migrations/* oauth2_lib/migrations
  3. Change oauth2_lib/migrations/0001_initial.py dependencies into
    dependencies = [
        migrations.swappable_dependency(settings.AUTH_USER_MODEL),
        migrations.swappable_dependency(oauth2_settings.APPLICATION_MODEL),	
        migrations.swappable_dependency(oauth2_settings.ACCESS_TOKEN_MODEL),	
        migrations.swappable_dependency(oauth2_settings.REFRESH_TOKEN_MODEL),	
        migrations.swappable_dependency(oauth2_settings.GRANT_MODEL),
    ]

@RenanMarcell up

@jarussi
Copy link

jarussi commented Aug 24, 2020

hey @Sharpek .. I tried your solution and couldn't get it to work. I got several other errors, but that's another issue.

What I'm worried about new migrations that django-oauth-toolkit might add. You'll have to keep updating the migrations manually by copying them from your virtualenv?

@jarussi
Copy link

jarussi commented Aug 24, 2020

Made it work here.
From the docs

Be aware that, when you intend to swap the application model, you should create and run the migration defining the swapped application model prior to setting OAUTH2_PROVIDER_APPLICATION_MODEL. You’ll run into models.E022 in Core system checks if you don’t get the order right.

You can force your migration providing the custom model to run in the right order by adding:

run_before = [
    ('oauth2_provider', '0001_initial'),
]
to the migration class.

That’s all, now Django OAuth Toolkit will use your model wherever an Application instance is needed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

5 participants