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

Method save

ormar/models/model.py:91–168  ·  view source on GitHub ↗

Performs a save of given Model instance. If primary key is already saved, db backend will throw integrity error. Related models are saved by pk number, reverse relation and many to many fields are not saved - use corresponding relations methods. If there ar

(self: T)

Source from the content-addressed store, hash-verified

89 return await self.update(**kwargs)
90
91 async def save(self: T) -> T:
92 """
93 Performs a save of given Model instance.
94 If primary key is already saved, db backend will throw integrity error.
95
96 Related models are saved by pk number, reverse relation and many to many fields
97 are not saved - use corresponding relations methods.
98
99 If there are fields with server_default set and those fields
100 are not already filled save will trigger also a second query
101 to refreshed the fields populated server side.
102
103 Does not recognize if model was previously saved.
104 If you want to perform update or insert depending on the pk
105 fields presence use upsert.
106
107 Sends pre_save and post_save signals.
108
109 Sets model save status to True.
110
111 :return: saved Model
112 :rtype: Model
113 """
114 await self._emit_signal("pre_save", instance=self)
115 self_fields = self._extract_model_db_fields()
116
117 if (
118 not self.pk
119 and self.ormar_config.model_fields[self.ormar_config.pkname].autoincrement
120 ):
121 self_fields.pop(self.ormar_config.pkname, None)
122 self_fields = self.populate_default_values(self_fields)
123 self.update_from_dict(
124 {
125 k: v
126 for k, v in self_fields.items()
127 if k not in self.extract_related_names()
128 }
129 )
130
131 self_fields = self.translate_columns_to_aliases(self_fields)
132 expr = self.ormar_config.table.insert()
133 expr = expr.values(**self_fields)
134
135 pkname = self.ormar_config.pkname
136 pk_returned_from_insert = False
137 pk = await self._execute_query(expr)
138 if pk and isinstance(pk, self.pk_type()):
139 setattr(self, pkname, pk)
140 pk_returned_from_insert = True
141
142 if self.pk is None:
143 raise ModelPersistenceError( # pragma: no cover
144 f"Could not recover the generated primary key for "
145 f"{self.__class__.__name__} after INSERT. This happens on "
146 "backends that lack RETURNING support for server-side "
147 "defaults on non-AUTO_INCREMENT primary keys — most notably "
148 "Oracle MySQL, which does not implement RETURNING in any "

Callers 15

upsertMethod · 0.95
create_itemFunction · 0.80
create_categoryFunction · 0.80
run_queryFunction · 0.80
verifyFunction · 0.80
test_typesFunction · 0.80
test_exclude_defaultFunction · 0.80
test_exclude_noneFunction · 0.80
test_exclude_unsetFunction · 0.80

Calls 14

_emit_signalMethod · 0.95
_execute_queryMethod · 0.95
loadMethod · 0.95
popMethod · 0.80
update_from_dictMethod · 0.80
extract_related_namesMethod · 0.80
pk_typeMethod · 0.80
set_save_statusMethod · 0.80

Tested by 15

test_typesFunction · 0.64
test_exclude_defaultFunction · 0.64
test_exclude_noneFunction · 0.64
test_exclude_unsetFunction · 0.64
create_userFunction · 0.64
create_user2Function · 0.64
create_user3Function · 0.64
create_user4Function · 0.64
create_user5Function · 0.64