MCPcopy
hub / github.com/piccolo-orm/piccolo / save

Method save

piccolo/table.py:490–546  ·  view source on GitHub ↗

A proxy to an insert or update query. :param columns: Only the specified columns will be synced back to the database when doing an update. For example: .. code-block:: python band = await Band.objects().first() b

(
        self, columns: Optional[Sequence[Union[Column, str]]] = None
    )

Source from the content-addressed store, hash-verified

488 ###########################################################################
489
490 def save(
491 self, columns: Optional[Sequence[Union[Column, str]]] = None
492 ) -> Union[Insert, Update]:
493 """
494 A proxy to an insert or update query.
495
496 :param columns:
497 Only the specified columns will be synced back to the database
498 when doing an update. For example:
499
500 .. code-block:: python
501
502 band = await Band.objects().first()
503 band.popularity = 2000
504 await band.save(columns=[Band.popularity])
505
506 If ``columns=None`` (the default) then all columns will be synced
507 back to the database.
508
509 """
510 cls = self.__class__
511
512 # New row - insert
513 if not self._exists_in_db:
514 return cls.insert(self).returning(cls._meta.primary_key)
515
516 # Pre-existing row - update
517 if columns is None:
518 column_instances = [
519 i for i in cls._meta.columns if not i._meta.primary_key
520 ]
521 else:
522 column_instances = [
523 self._meta.get_column_by_name(i) if isinstance(i, str) else i
524 for i in columns
525 ]
526
527 values: dict[Column, Any] = {
528 i: getattr(self, i._meta.name, None) for i in column_instances
529 }
530
531 # Assign any `auto_update` values
532 if cls._meta.auto_update_columns:
533 auto_update_values = cls._meta.get_auto_update_values()
534 values.update(auto_update_values)
535 for column, value in auto_update_values.items():
536 setattr(self, column._meta.name, value)
537
538 return cls.update(
539 values, # type: ignore
540 # We've already included the `auto_update` columns, so no need
541 # to do it again:
542 use_auto_update=False,
543 ).where(
544 cls._meta.primary_key
545 == getattr(self, self._meta.primary_key._meta.name)
546 )
547

Callers 15

create_userMethod · 0.80
populateFunction · 0.80
runMethod · 0.80
runMethod · 0.80
_buildMethod · 0.80
__exit__Method · 0.80
test_uuid_formatMethod · 0.80
test_loginMethod · 0.80
test_update_passwordMethod · 0.80
test_createMethod · 0.80
setUpMethod · 0.80
insert_rowsMethod · 0.80

Calls 6

insertMethod · 0.80
get_column_by_nameMethod · 0.80
updateMethod · 0.80
returningMethod · 0.45
whereMethod · 0.45

Tested by 15

test_uuid_formatMethod · 0.64
test_loginMethod · 0.64
test_update_passwordMethod · 0.64
test_createMethod · 0.64
setUpMethod · 0.64
insert_rowsMethod · 0.64
_run_comparisonMethod · 0.64
setUpMethod · 0.64
run_nestedMethod · 0.64
run_nestedMethod · 0.64
_make_queryMethod · 0.64
_make_many_queriesMethod · 0.64