django-oauth-toolkit icon indicating copy to clipboard operation
django-oauth-toolkit copied to clipboard

Migration dependency issue if extending AbstractApplication

Open anveshagarwal opened this issue 5 years ago • 7 comments

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 https://github.com/jazzband/django-oauth-toolkit/pull/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

anveshagarwal avatar Jan 21 '20 09:01 anveshagarwal

Same here ;(

Sharpek avatar Feb 18 '20 12:02 Sharpek

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?

RenanMarcell avatar Apr 22 '20 17:04 RenanMarcell

+1

wojciak avatar May 07 '20 14:05 wojciak

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

Sharpek avatar May 07 '20 15:05 Sharpek

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 avatar Aug 24 '20 12:08 jarussi

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.

jarussi avatar Aug 24 '20 21:08 jarussi

delete all migrations file from your apps and delete oauth2_provider migrations from venv/Lib/site-packages/oauth2_provider/migrations

then run makemigrations and migrate again

masihvaziri avatar Jun 22 '24 09:06 masihvaziri