Skip to content

Commit

Permalink
Proof of concept of db_default uuid4() #10958
Browse files Browse the repository at this point in the history
  • Loading branch information
jacobtylerwalls committed Dec 3, 2024
1 parent 449ef94 commit 4011a48
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 3 deletions.
7 changes: 7 additions & 0 deletions arches/app/models/functions.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
from django.db import models


class UUID4(models.Func):
function = "uuid_generate_v4"
arity = 0
output_field = models.UUIDField()
24 changes: 24 additions & 0 deletions arches/app/models/migrations/11045_generate_resource_ids.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Generated by Django 5.1.3 on 2024-11-06 12:10

import arches.app.models.functions
from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
("models", "11044_make_further_fields_blank"),
]

operations = [
migrations.AlterField(
model_name="resourceinstance",
name="resourceinstanceid",
field=models.UUIDField(
blank=True,
db_default=arches.app.models.functions.UUID4(),
primary_key=True,
serialize=False,
),
),
]
7 changes: 4 additions & 3 deletions arches/app/models/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
from arches.app.utils.module_importer import get_class_from_modulename
from arches.app.utils.thumbnail_factory import ThumbnailGeneratorInstance
from arches.app.models.fields.i18n import I18n_TextField, I18n_JSONField
from arches.app.models.functions import UUID4
from arches.app.models.querysets import ResourceInstanceQuerySet, TileQuerySet
from arches.app.models.utils import (
add_to_update_fields,
Expand Down Expand Up @@ -1205,7 +1206,9 @@ class Meta:


class ResourceInstance(models.Model):
resourceinstanceid = models.UUIDField(primary_key=True, blank=True)
resourceinstanceid = models.UUIDField(
primary_key=True, blank=True, db_default=UUID4()
)
graph = models.ForeignKey(GraphModel, db_column="graphid", on_delete=models.CASCADE)
graph_publication = models.ForeignKey(
GraphXPublishedGraph,
Expand Down Expand Up @@ -1248,8 +1251,6 @@ def __init__(self, *args, **kwargs):

for kwarg, value in arches_model_kwargs.items():
setattr(self, kwarg, value)
if not self.resourceinstanceid:
self.resourceinstanceid = uuid.uuid4()

def __repr__(self):
return f"<{self.graph.name}: {self.name} ({self.pk})>"
Expand Down

0 comments on commit 4011a48

Please sign in to comment.