Skip to content

Commit 59025cb

Browse files
committed
writing: fix code blocks in sqlalchemy section
1 parent eca0778 commit 59025cb

File tree

3 files changed

+19
-4
lines changed

3 files changed

+19
-4
lines changed

docs/docs/06_sql_storage_sqlalchemy/05_insert_models_sqlalchemy/README.md

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,13 @@ description: Learn how to use SQLAlchemy to add new rows to our SQL database.
1414
Inserting models with SQLAlchemy couldn't be easier! We'll use the `db.session`[^1] variable to `.add()` a model. Let's begin working on our `Item` resource:
1515

1616
```python title="resources/item.py"
17+
from sqlalchemy.exc import SQLAlchemyError
18+
19+
from db import db
20+
from models import ItemModel
21+
22+
...
23+
1724
@blp.arguments(ItemSchema)
1825
@blp.response(201, ItemSchema)
1926
def post(self, item_data):
@@ -31,6 +38,13 @@ def post(self, item_data):
3138
Similarly in our `Store` resource:
3239

3340
```python title="resources/store.py"
41+
from sqlalchemy.exc import SQLAlchemyError, IntegrityError
42+
43+
from db import db
44+
from models import StoreModel
45+
46+
...
47+
3448
@blp.arguments(StoreSchema)
3549
@blp.response(201, StoreSchema)
3650
def post(self, store_data):

docs/docs/06_sql_storage_sqlalchemy/07_updating_models_sqlalchemy/README.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,17 @@ This idempotency is frequently seen with `PUT` requests. You can see it in actio
2121
@blp.arguments(ItemUpdateSchema)
2222
@blp.response(200, ItemSchema)
2323
def put(self, item_data, item_id):
24-
item = ItemModel.query.get_or_404(item_id)
2524
# highlight-start
25+
item = ItemModel.query.get(item_id)
2626
if item:
2727
item.price = item_data["price"]
2828
item.name = item_data["name"]
2929
else:
30-
item = ItemModel(**item_data)
30+
item = ItemModel(id=item_id, **item_data)
3131

3232
db.session.add(item)
3333
db.session.commit()
3434

3535
return item
3636
# highlight-end
37-
```
37+
```

docs/docs/06_sql_storage_sqlalchemy/10_conclusion/README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ class ItemModel(db.Model):
3030
__tablename__ = "items"
3131

3232
id = db.Column(db.Integer, primary_key=True)
33-
name = db.Column(db.String(80), unique=True, nullable=False)
33+
name = db.Column(db.String(80), unique=False, nullable=False)
3434
price = db.Column(db.Float(precision=2), unique=False, nullable=False)
3535

3636
store_id = db.Column(
@@ -92,6 +92,7 @@ class ItemSchema(PlainItemSchema):
9292
class ItemUpdateSchema(Schema):
9393
name = fields.Str()
9494
price = fields.Float()
95+
store_id = fields.Int()
9596

9697

9798
class StoreSchema(PlainStoreSchema):

0 commit comments

Comments
 (0)