Commit e8cb7b77 by Tyler Hallada

Custom one-to-one field for ref to custom id field

parent 6b97c1bd
...@@ -3,6 +3,7 @@ Custom fields for use in the coursewarehistoryextended django app. ...@@ -3,6 +3,7 @@ Custom fields for use in the coursewarehistoryextended django app.
""" """
from django.db.models.fields import AutoField from django.db.models.fields import AutoField
from django.db.models.fields.related import OneToOneField
class UnsignedBigIntAutoField(AutoField): class UnsignedBigIntAutoField(AutoField):
...@@ -24,6 +25,7 @@ class UnsignedBigIntAutoField(AutoField): ...@@ -24,6 +25,7 @@ class UnsignedBigIntAutoField(AutoField):
else: else:
return None return None
# rel_db_type was added in Django 1.10. For versions before, use UnsignedBigIntOneToOneField.
def rel_db_type(self, connection): def rel_db_type(self, connection):
if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql': if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
return "bigint UNSIGNED" return "bigint UNSIGNED"
...@@ -33,3 +35,20 @@ class UnsignedBigIntAutoField(AutoField): ...@@ -33,3 +35,20 @@ class UnsignedBigIntAutoField(AutoField):
return "BIGSERIAL" return "BIGSERIAL"
else: else:
return None return None
class UnsignedBigIntOneToOneField(OneToOneField):
"""
An unsigned 8-byte integer one-to-one foreign key to a unsigned 8-byte integer id field.
Should only be necessary for versions of Django < 1.10.
"""
def db_type(self, connection):
if connection.settings_dict['ENGINE'] == 'django.db.backends.mysql':
return "bigint UNSIGNED"
elif connection.settings_dict['ENGINE'] == 'django.db.backends.sqlite3':
return "integer"
elif connection.settings_dict['ENGINE'] == 'django.db.backends.postgresql_psycopg2':
return "BIGSERIAL"
else:
return None
...@@ -3,6 +3,8 @@ from __future__ import unicode_literals ...@@ -3,6 +3,8 @@ from __future__ import unicode_literals
from django.db import migrations, models from django.db import migrations, models
from coursewarehistoryextended.fields import UnsignedBigIntOneToOneField
class Migration(migrations.Migration): class Migration(migrations.Migration):
...@@ -21,7 +23,7 @@ class Migration(migrations.Migration): ...@@ -21,7 +23,7 @@ class Migration(migrations.Migration):
('possible_all_override', models.FloatField(null=True, blank=True)), ('possible_all_override', models.FloatField(null=True, blank=True)),
('earned_graded_override', models.FloatField(null=True, blank=True)), ('earned_graded_override', models.FloatField(null=True, blank=True)),
('possible_graded_override', models.FloatField(null=True, blank=True)), ('possible_graded_override', models.FloatField(null=True, blank=True)),
('grade', models.OneToOneField(related_name='override', to='grades.PersistentSubsectionGrade')), ('grade', UnsignedBigIntOneToOneField(related_name='override', to='grades.PersistentSubsectionGrade')),
], ],
), ),
] ]
...@@ -20,7 +20,7 @@ from lazy import lazy ...@@ -20,7 +20,7 @@ from lazy import lazy
from model_utils.models import TimeStampedModel from model_utils.models import TimeStampedModel
from opaque_keys.edx.keys import CourseKey, UsageKey from opaque_keys.edx.keys import CourseKey, UsageKey
from coursewarehistoryextended.fields import UnsignedBigIntAutoField from coursewarehistoryextended.fields import UnsignedBigIntAutoField, UnsignedBigIntOneToOneField
from eventtracking import tracker from eventtracking import tracker
from openedx.core.djangoapps.xmodule_django.models import CourseKeyField, UsageKeyField from openedx.core.djangoapps.xmodule_django.models import CourseKeyField, UsageKeyField
from request_cache import get_cache from request_cache import get_cache
...@@ -693,7 +693,7 @@ class PersistentSubsectionGradeOverride(models.Model): ...@@ -693,7 +693,7 @@ class PersistentSubsectionGradeOverride(models.Model):
class Meta(object): class Meta(object):
app_label = "grades" app_label = "grades"
grade = models.OneToOneField(PersistentSubsectionGrade, related_name='override') grade = UnsignedBigIntOneToOneField(PersistentSubsectionGrade, related_name='override')
# Created/modified timestamps prevent race-conditions when using with async rescoring tasks # Created/modified timestamps prevent race-conditions when using with async rescoring tasks
created = models.DateTimeField(auto_now_add=True, db_index=True) created = models.DateTimeField(auto_now_add=True, db_index=True)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment