8000 add bulk_create to QuerySet for batched inserts by asnelling · Pull Request #71 · encode/orm · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

add bulk_create to QuerySet for batched inserts #71

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

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
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
8 changes: 7 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,6 @@ class Note(orm.Model):
__tablename__ = "notes"
__database__ = database
__metadata__ = metadata

id = orm.Integer(primary_key=True)
text = orm.String(max_length=100)
completed = orm.Boolean(default=False)
Expand All @@ -53,6 +52,13 @@ await Note.objects.create(text="Buy the groceries.", completed=False)
await Note.objects.create(text="Call Mum.", completed=True)
await Note.objects.create(text="Send invoices.", completed=True)

# .bulk_create()
await Note.objects.bulk_create([
Note(text="Buy the groceries.", completed=False),
Note(text="Call Mum.", completed=True),
Note(text="Send invoices.", completed=True),
])

# .all()
notes = await Note.objects.all()

Expand Down
22 changes: 22 additions & 0 deletions orm/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,28 @@ async def create(self, **kwargs):
instance.pk = await self.database.execute(expr)
return instance

async def bulk_create(self, objs):
# Validate the keyword arguments.
fields = self.model_cls.fields
required = [key for key, value in fields.items() if not value.has_default()]
validator = typesystem.Object(
properties=fields, required=required, additional_properties=False
)
validated_objs = [validator.validate(o) for o in objs]

# Remove primary key when None to prevent not null constraint in postgresql.
pkname = self.model_cls.__pkname__
pk = self.model_cls.fields[pkname]
for obj in validated_objs:
if obj[pkname] is None and pk.allow_null:
del obj[pkname]

# Build the insert expression.
expr = self.table.insert()

# Execute the insert, and return a new model instance.
return await self.database.execute_many(expr, validated_objs)


class Model(typesystem.Schema, metaclass=ModelMetaclass):
__abstract__ = True
Expand Down
12 changes: 12 additions & 0 deletions tests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,15 @@ async def test_model_first():
assert await User.objects.first(name="Jane") == jane
assert await User.objects.filter(name="Jane").first() == jane
assert await User.objects.filter(name="Lucy").first() is None


@async_adapter
async def test_model_bulk_create():
async with database:
await User.objects.bulk_create([
User(name="Tom"),
User(name="Jane"),
User(name="Lucy"),
])

assert await User.objects.count() == 3
0