You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/docs/06_sql_storage_sqlalchemy/05_insert_models_sqlalchemy/README.md
+15-15Lines changed: 15 additions & 15 deletions
Original file line number
Diff line number
Diff line change
@@ -32,21 +32,21 @@ Similarly in our `Store` resource:
32
32
33
33
```python title="resources/store.py"
34
34
@blp.arguments(StoreSchema)
35
-
@blp.response(201, StoreSchema)
36
-
defpost(self, store_data):
37
-
store = StoreModel(**store_data)
38
-
try:
39
-
db.session.add(store)
40
-
db.session.commit()
41
-
except IntegrityError:
42
-
abort(
43
-
400,
44
-
message="A store with that name already exists.",
45
-
)
46
-
except SQLAlchemyError:
47
-
abort(500, message="An error occurred creating the store.")
48
-
49
-
return store
35
+
@blp.response(201, StoreSchema)
36
+
defpost(self, store_data):
37
+
store = StoreModel(**store_data)
38
+
try:
39
+
db.session.add(store)
40
+
db.session.commit()
41
+
except IntegrityError:
42
+
abort(
43
+
400,
44
+
message="A store with that name already exists.",
45
+
)
46
+
except SQLAlchemyError:
47
+
abort(500, message="An error occurred creating the store.")
48
+
49
+
return store
50
50
```
51
51
52
52
Note here we're catching two different errors, `IntegrityError` for when a client attempts to create a store with a name that already exists, and `SQLAlchemyError` for anything else.
0 commit comments