pythonintermediate
SQLModel CRUD Patterns
Use SQLModel (SQLAlchemy + Pydantic) to define models and run type-safe CRUD operations.
pythonPress ⌘/Ctrl + Shift + C to copy
from sqlmodel import SQLModel, Field, Session, create_engine, select
from typing import Optional
class Hero(SQLModel, table=True):
id: Optional[int] = Field(default=None, primary_key=True)
name: str
age: int
engine = create_engine('sqlite:///heroes.db')
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
session.add(Hero(name='Spider-Man', age=18))
session.add(Hero(name='Iron Man', age=45))
session.commit()
with Session(engine) as session:
heroes = session.exec(select(Hero).where(Hero.age > 20)).all()
for h in heroes:
print(h)Use Cases
- FastAPI backends
- typed ORM
- CRUD APIs
Tags
Related Snippets
Similar patterns you can reuse in the same workflow.
typescriptintermediate
Prisma Find with Relations
Query related records using Prisma ORM include and select for efficient nested data loading.
Best for: Loading user profiles with posts
#prisma#orm
pythonintermediate
FastAPI Dependency Injection
Use FastAPI's dependency injection for database sessions, auth checks, and shared service logic.
Best for: Database session management
#fastapi#dependency-injection
pythonintermediate
OpenAI Streaming with SSE in FastAPI
Stream OpenAI responses as Server-Sent Events from a FastAPI endpoint.
Best for: streaming AI APIs
#openai#fastapi
javaintermediate
Spring Boot REST Controller with CRUD
Create a complete REST API with Spring Boot: GET, POST, PUT, DELETE with validation and error handling.
Best for: Building RESTful APIs with Spring Boot
#spring-boot#rest-api