Skip to content

Commit

Permalink
Merge pull request #845 from circulon/feature/bulk_create_no_uuid_pk
Browse files Browse the repository at this point in the history
Fixed UUID PK Mixin not creating primary key on bulk create
  • Loading branch information
josephmancuso authored Sep 30, 2023
2 parents a1121ce + 013ccca commit 1edbb44
Showing 1 changed file with 19 additions and 9 deletions.
28 changes: 19 additions & 9 deletions src/masoniteorm/scopes/UUIDPrimaryKeyScope.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import uuid

from .BaseScope import BaseScope


Expand All @@ -9,6 +10,9 @@ def on_boot(self, builder):
builder.set_global_scope(
"_UUID_primary_key", self.set_uuid_create, action="insert"
)
builder.set_global_scope(
"_UUID_primary_key", self.set_bulk_uuid_create, action="bulk_create"
)

def on_remove(self, builder):
pass
Expand All @@ -22,15 +26,21 @@ def generate_uuid(self, builder, uuid_version, bytes=False):

return uuid_func(*args).bytes if bytes else str(uuid_func(*args))

def build_uuid_pk(self, builder):
uuid_version = getattr(builder._model, "__uuid_version__", 4)
uuid_bytes = getattr(builder._model, "__uuid_bytes__", False)
return {
builder._model.__primary_key__: self.generate_uuid(
builder, uuid_version, uuid_bytes
)
}

def set_uuid_create(self, builder):
# if there is already a primary key, no need to set a new one
if builder._model.__primary_key__ not in builder._creates:
uuid_version = getattr(builder._model, "__uuid_version__", 4)
uuid_bytes = getattr(builder._model, "__uuid_bytes__", False)
builder._creates.update(
{
builder._model.__primary_key__: self.generate_uuid(
builder, uuid_version, uuid_bytes
)
}
)
builder._creates.update(self.build_uuid_pk(builder))

def set_bulk_uuid_create(self, builder):
for idx, create_atts in enumerate(builder._creates):
if builder._model.__primary_key__ not in create_atts:
builder._creates[idx].update(self.build_uuid_pk(builder))

0 comments on commit 1edbb44

Please sign in to comment.