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

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

Open michaeltcoelho opened this issue 7 years ago • 8 comments
trafficstars

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.

michaeltcoelho avatar Jan 18 '18 14:01 michaeltcoelho

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.

rhymiz avatar Jan 24 '18 02:01 rhymiz

Thanks @rhymiz

michaeltcoelho avatar Jan 25 '18 16:01 michaeltcoelho

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.

michaeltcoelho avatar Jan 25 '18 16:01 michaeltcoelho

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

rhymiz avatar Jan 25 '18 18:01 rhymiz

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

douglashall avatar Jun 21 '18 18:06 douglashall

@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.

rhymiz avatar Jun 21 '18 20:06 rhymiz

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?

iamhssingh avatar Dec 26 '22 10:12 iamhssingh

@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.

rhymiz avatar Jan 04 '23 16:01 rhymiz

I'm closing since I am unable to reproduce this. If you want to re-open Please create a gist that we can use to reproduce the issue.

dopry avatar Nov 02 '25 17:11 dopry