Just here to say that writing SQL backed REST APIs with @tiangolo's @FastAPI and SQLModel is easier than falling off a log!
sqlite_url='sqlite:///' + sqlite_file_name
engine = create_engine(sqlite_url)
engine = create_engine(sqlite_url, echo=True)
SQLModel.metadata.create_all(engine)
app = FastAPI()
@app.get("/player/create/{player_name}")
def create_player(player_name: str):
with Session(engine) as session:
player = Player(name=player_name)
session.add(player)
session.commit()
@app.get("/player/delete/{player_id}")
def delete_player(player_id: int):
with Session(engine) as session:
player = session.get(Player, player_id)
session.delete(player)
session.commit()
@app.get("/player/get/{player_id}")
def get_player(player_id: int):
with Session(engine) as session:
player = session.get(Player, player_id)
return player
@app.get("/thing/create/{thing_name}")
def create_thing(thing_name: str):
with Session(engine) as session:
thing = Thing(name=thing_name)
session.add(thing)
session.commit()
@app.get("/thing/get/{thing_id}")
def get_thing(thing_id: int):
with Session(engine) as session:
thing = session.get(Thing, thing_id)
return thing
The people who say #python is so 5 minutes ago aren't paying attention. The community just keeps ratcheting up the level of awesome, and I am incredibly grateful for all the hard work everyone has put in!
=> More informations about this toot | More toots from feoh@oldbytes.space
And because it's all based on @pydantic you get constraint violation checks for free!
ERROR Exception in ASGI application
Traceback (most recent call last):
File "/home/feoh/src/personal/chrismud/.venv/lib/python3.12/site-packages/sqlalchemy/engine/base.py", line 1964, in _exec_single_context
self.dialect.do_execute(
File "/home/feoh/src/personal/chrismud/.venv/lib/python3.12/site-packages/sqlalchemy/engine/default.py", line 942, in do_execute
cursor.execute(statement, parameters)
sqlite3.IntegrityError: NOT NULL constraint failed: player.location
=> More informations about this toot | More toots from feoh@oldbytes.space
@feoh I have to say I love the way this code formatting looks 🙂
=> More informations about this toot | More toots from diazona@techhub.social
@diazona Huh! :)
Just standard Markdown, three backticks:
=> More informations about this toot | More toots from feoh@oldbytes.space
@feoh Nice 🙂 I was thinking of the choice of background color and font and such in how it's rendered... which I guess might be client specific. But I am also a little wistful that I don't get this on stock Mastodon. What server software is your instance using? I might have to think about switching.
=> More informations about this toot | More toots from diazona@techhub.social
@diazona Mastodon Glitch Edition: https://glitch-soc.github.io/docs/
=> More informations about this toot | More toots from feoh@oldbytes.space
@feoh It sounds great that SQLModel's model is also a Pydantic model.
=> More informations about this toot | More toots from veer66@vivaldi.net This content has been proxied by September (ba2dc).Proxy Information
text/gemini