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

ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference #546

Open
michaeltcoelho opened this issue Jan 18, 2018 · 8 comments

Comments

@michaeltcoelho
Copy link

michaeltcoelho commented Jan 18, 2018

Hi there,

I'm running makemigrations oauthy on Django==1.11 after extending AbstractApplication but with no success. I keep getting the error:

ValueError: The field oauth2_provider.AccessToken.application was declared with a lazy reference to 'oauthy.client', but app 'oauthy' isn't installed.
The field oauth2_provider.Grant.application was declared with a lazy reference to 'oauthy.client', but app 'oauthy' isn't installed.
The field oauth2_provider.RefreshToken.application was declared with a lazy reference to 'oauthy.client', but app 'oauthy' isn't installed.

Here is my models.py:

def content_file_name(instance, filename):
    return '/'.join(['oauth', 'clients', str(instance.client_id), filename])


class Client(AbstractApplication):
    logo = models.ImageField(u'logo', upload_to=content_file_name, null=True, blank=True)

I tried everything i've found on the web.. but most problems is about running migrate not makemigrations.

django-oauth-toolkit==1.0.0
oauthlib==2.0.6

Thanks in advance.

@rhymiz
Copy link

rhymiz commented Jan 24, 2018

You first need to run a migration without your custom app model.
Once the initial migration has been done, you can swap the default Application model for yours and your migration should succeed.

@michaeltcoelho
Copy link
Author

Thanks @rhymiz

@michaeltcoelho
Copy link
Author

michaeltcoelho commented Jan 25, 2018

I tried your advice and it worked. But now, i'm getting (1452, 'Cannot add or update a child row: a foreign key constraint fails (db.oauth2_provider_grant, CONSTRAINT oauth2_provider_grant_application_id_81923564_fk FOREIGN KEY (application_id) REFERENCES oauth2_provider_application (id))') when authorizing a client.

@rhymiz
Copy link

rhymiz commented Jan 25, 2018

In settings.py you have something like the line below, correct? OAUTH2_PROVIDER_APPLICATION_MODEL='yourapp.Client'

@douglashall
Copy link

How will this work in environments where application setup needs to be repeatable? For instance CI testing environments, etc.

@rhymiz
Copy link

rhymiz commented Jun 21, 2018

@douglashall during the setup process in CI environments, you could use some sort of schema.sql file to create all the basic tables you need before any other migration and testing steps.

@iamhssingh
Copy link

You first need to run a migration without your custom app model. Once the initial migration has been done, you can swap the default Application model for yours and your migration should succeed.

I think this breaks down on squashing migrations. What purpose does running an initial migration serve?

@rhymiz
Copy link

rhymiz commented Jan 4, 2023

@iamhssingh I believe the initial version of toolkit did not have a swappable application model, so migrations needs to be applied in a specific order -- don't quote me on this, though.

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

4 participants