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

AuthTokenManager.get_or_create calls wrong create #18

Open
hodunov opened this issue Jun 6, 2021 · 4 comments
Open

AuthTokenManager.get_or_create calls wrong create #18

hodunov opened this issue Jun 6, 2021 · 4 comments
Labels
bug Something isn't working question Further information is requested

Comments

@hodunov
Copy link

hodunov commented Jun 6, 2021

Hi!
Thanks for the library, it really helped me solve my problem. But while implementing authorization via GraphQL I encountered the following problem: AuthToken.objects.get_or_create(user=user, client=auth_client) method does not work correctly.
Expected behavior: classic get_or_create
Real result: error NOT NULL constraint failed: durin_authtoken.expiry

A piece of code in which an error occurs:

login(request, user)
token, created = AuthToken.objects.get_or_create(user=user, client=auth_client)

If I wrap it in try-exept, everything works as it should.

try:
    token = AuthToken.objects.get(user=user, client=auth_client)
except AuthToken.DoesNotExist:
    token = AuthToken.objects.create(user=user, client=auth_client)
@eshaan7
Copy link
Owner

eshaan7 commented Jun 8, 2021

Hi, @hodunov. It makes me glad that you found this library useful.

So I can better address this issue, can you let me know the python and django versions your project is using ?

@eshaan7 eshaan7 added the question Further information is requested label Jun 8, 2021
@eshaan7
Copy link
Owner

eshaan7 commented Jun 8, 2021

If you see the code inside durin/models.py, we have a custom AuthTokenManager where the create method is overwritten. It seems as though that the get_or_create in your example is calling the default create and not the overwritten create of AuthTokenManager.

On digging more into this, I find that this was fixed in Django v1.8a1 as mentioned in this StackOverflow thread, quoted below,

Just want to clarify that this issue was addressed in Django v1.8a1. So now both get_or_create and update_or_create will call the managers create method as expected.

@hodunov
Copy link
Author

hodunov commented Jun 8, 2021

Hi, @hodunov. It makes me glad that you found this library useful.
So I can better address this issue, can you let me know the python and django versions your project is using ?

Yes, of course, no problem. Django==3.2 with python 3.8

Absolutely right, rushed and did not describe my thoughts on this matter. I agree that the reason is exactly in models.Manager.
And yes, I saw that get_or_create method should work with manager, but it doesn't, check it yourself.

@sunweiyang
Copy link

I have an interesting situation too -- AuthToken.objects.get_or_create works in most cases, but when I'm performing Django unit tests, AuthToken.objects.get_or_create within the API views I'm testing doesn't work (but they do work in the unit tests themselves, before I call the API views!).

@eshaan7 eshaan7 added the bug Something isn't working label Sep 30, 2021
@eshaan7 eshaan7 changed the title [Error] NOT NULL constraint failed: durin_authtoken.expiry AuthTokenManager.get_or_create calls wrong create Oct 20, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working question Further information is requested
Projects
None yet
Development

No branches or pull requests

3 participants