Skip to content

Commit

Permalink
fix: UUIDField must subclass AutoField
Browse files Browse the repository at this point in the history
  • Loading branch information
milahu committed Feb 3, 2024
1 parent babd273 commit 2e9a1f4
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 6 deletions.
2 changes: 1 addition & 1 deletion archivebox/core/apps.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@
class CoreConfig(AppConfig):
name = 'core'
# WIP: broken by Django 3.1.2 -> 4.0 migration
default_auto_field = 'django.db.models.UUIDField'
default_auto_field = 'django.db.models.UUIDAutoField'
2 changes: 1 addition & 1 deletion archivebox/core/migrations/0001_initial.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class Migration(migrations.Migration):
migrations.CreateModel(
name='Snapshot',
fields=[
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('id', models.UUIDAutoField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
('url', models.URLField(unique=True)),
('timestamp', models.CharField(default=None, max_length=32, null=True, unique=True)),
('title', models.CharField(default=None, max_length=128, null=True)),
Expand Down
2 changes: 1 addition & 1 deletion archivebox/core/migrations/0011_auto_20210216_1331.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class Migration(migrations.Migration):
migrations.AddField(
model_name='archiveresult',
name='uuid',
field=models.UUIDField(default=uuid.uuid4, editable=False),
field=models.UUIDAutoField(default=uuid.uuid4, editable=False),
),
migrations.AlterField(
model_name='archiveresult',
Expand Down
27 changes: 25 additions & 2 deletions archivebox/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,29 @@
import jsonfield
JSONField = jsonfield.JSONField

# fix: ValueError: Primary key 'django.db.models.UUIDField' referred by core.apps.CoreConfig.default_auto_field must subclass AutoField.
# based on https://code.djangoproject.com/ticket/32577
# also used in default_auto_field and DEFAULT_AUTO_FIELD

class UUIDAutoField(models.fields.AutoField, models.fields.UUIDField):

def __init__(self, *args, **kwargs):
#kwargs['db_default'] = RandomUUID()
#kwargs['db_default'] = uuid.uuid4()
super().__init__(*args, **kwargs)

def deconstruct(self):
name, path, args, kwargs = super().deconstruct()
#del kwargs['db_default']
return name, path, args, kwargs

def get_internal_type(self):
return 'UUIDAutoField'

def rel_db_type(self, connection):
return UUIDField().db_type(connection=connection)

models.UUIDAutoField = UUIDAutoField

class Tag(models.Model):
"""
Expand Down Expand Up @@ -85,7 +108,7 @@ def save(self, *args, **kwargs):


class Snapshot(models.Model):
id = models.UUIDField(primary_key=True, default=uuid.uuid4, editable=False)
id = models.UUIDAutoField(primary_key=True, default=uuid.uuid4, editable=False)

url = models.URLField(unique=True, db_index=True)
timestamp = models.CharField(max_length=32, unique=True, db_index=True)
Expand Down Expand Up @@ -268,7 +291,7 @@ def indexable(self, sorted: bool = True):

class ArchiveResult(models.Model):
id = models.AutoField(primary_key=True, serialize=False, verbose_name='ID')
uuid = models.UUIDField(default=uuid.uuid4, editable=False)
uuid = models.UUIDAutoField(default=uuid.uuid4, editable=False)

snapshot = models.ForeignKey(Snapshot, on_delete=models.CASCADE)
extractor = models.CharField(choices=EXTRACTORS, max_length=32)
Expand Down
2 changes: 1 addition & 1 deletion archivebox/core/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@
]

# WIP: broken by Django 3.1.2 -> 4.0 migration
DEFAULT_AUTO_FIELD = 'django.db.models.UUIDField'
DEFAULT_AUTO_FIELD = 'django.db.models.UUIDAutoField'

################################################################################
### Shell Settings
Expand Down

0 comments on commit 2e9a1f4

Please sign in to comment.