Skip to content

Commit bb70d85

Browse files
jimgrahamsynasius
authored andcommitted
Add unique constraints to models' token codes
Add a unique constraint to the token codes to avoid `objects.get() MultipleObjectsReturned` exceptions.
1 parent 86ef077 commit bb70d85

File tree

2 files changed

+32
-3
lines changed

2 files changed

+32
-3
lines changed
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import migrations, models
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('oauth2_provider', '0003_auto_20160316_1503'),
11+
]
12+
13+
operations = [
14+
migrations.AlterField(
15+
model_name='accesstoken',
16+
name='token',
17+
field=models.CharField(unique=True, max_length=255),
18+
),
19+
migrations.AlterField(
20+
model_name='grant',
21+
name='code',
22+
field=models.CharField(unique=True, max_length=255),
23+
),
24+
migrations.AlterField(
25+
model_name='refreshtoken',
26+
name='token',
27+
field=models.CharField(unique=True, max_length=255),
28+
),
29+
]

oauth2_provider/models.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ class Grant(models.Model):
149149
* :attr:`scope` Required scopes, optional
150150
"""
151151
user = models.ForeignKey(settings.AUTH_USER_MODEL)
152-
code = models.CharField(max_length=255, db_index=True) # code comes from oauthlib
152+
code = models.CharField(max_length=255, unique=True) # code comes from oauthlib
153153
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL)
154154
expires = models.DateTimeField()
155155
redirect_uri = models.CharField(max_length=255)
@@ -186,7 +186,7 @@ class AccessToken(models.Model):
186186
* :attr:`scope` Allowed scopes
187187
"""
188188
user = models.ForeignKey(settings.AUTH_USER_MODEL, blank=True, null=True)
189-
token = models.CharField(max_length=255, db_index=True)
189+
token = models.CharField(max_length=255, unique=True)
190190
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL)
191191
expires = models.DateTimeField()
192192
scope = models.TextField(blank=True)
@@ -255,7 +255,7 @@ class RefreshToken(models.Model):
255255
bounded to
256256
"""
257257
user = models.ForeignKey(settings.AUTH_USER_MODEL)
258-
token = models.CharField(max_length=255, db_index=True)
258+
token = models.CharField(max_length=255, unique=True)
259259
application = models.ForeignKey(oauth2_settings.APPLICATION_MODEL)
260260
access_token = models.OneToOneField(AccessToken,
261261
related_name='refresh_token')

0 commit comments

Comments
 (0)