MCPcopy Create free account
hub / github.com/tortoise/tortoise-orm / bulk_create

Method bulk_create

tortoise/queryset.py:874–915  ·  view source on GitHub ↗

This method inserts the provided list of objects into the database in an efficient manner (generally only 1 query, no matter how many objects there are). Fields with ``db_default`` that are not explicitly set will use the database DEFAULT. Within a single call, eac

(
        self,
        objects: Iterable[MODEL],
        batch_size: int | None = None,
        ignore_conflicts: bool = False,
        update_fields: Iterable[str] | None = None,
        on_conflict: Iterable[str] | None = None,
    )

Source from the content-addressed store, hash-verified

872 return {getattr(obj, field_name): obj for obj in objs}
873
874 def bulk_create(
875 self,
876 objects: Iterable[MODEL],
877 batch_size: int | None = None,
878 ignore_conflicts: bool = False,
879 update_fields: Iterable[str] | None = None,
880 on_conflict: Iterable[str] | None = None,
881 ) -> BulkCreateQuery[MODEL]:
882 """
883 This method inserts the provided list of objects into the database in an efficient manner
884 (generally only 1 query, no matter how many objects there are).
885
886 Fields with ``db_default`` that are not explicitly set will use the database
887 DEFAULT. Within a single call, each ``db_default`` field must be treated
888 consistently: either **all** instances supply a value or **none** do.
889
890 :param on_conflict: On conflict index name
891 :param update_fields: Update fields when conflicts
892 :param ignore_conflicts: Ignore conflicts when inserting
893 :param objects: List of objects to bulk create
894 :param batch_size: How many objects are created in a single query
895
896 :raises ValueError: If params do not meet specifications
897 :raises OperationalError: If a ``db_default`` field has mixed usage across
898 instances (some provide a value, others rely on the database default).
899 """
900 if ignore_conflicts and update_fields:
901 raise ValueError(
902 "ignore_conflicts and update_fields are mutually exclusive.",
903 )
904 if not ignore_conflicts:
905 if (update_fields and not on_conflict) or (on_conflict and not update_fields):
906 raise ValueError("update_fields and on_conflict need set in same time.")
907 return BulkCreateQuery(
908 db=self._db,
909 model=self.model,
910 objects=objects,
911 batch_size=batch_size,
912 ignore_conflicts=ignore_conflicts,
913 update_fields=update_fields,
914 on_conflict=on_conflict,
915 )
916
917 def bulk_update(
918 self,

Callers

nothing calls this directly

Calls 1

BulkCreateQueryClass · 0.85

Tested by

no test coverage detected