Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed UUID PK Mixin not creating primary key on bulk create #845

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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))