FastCRUD

raw JSON →
0.21.0 verified Mon Apr 27 auth: no python

FastCRUD is a Python package for FastAPI, offering robust async CRUD operations and flexible endpoint creation utilities. Built on SQLAlchemy, it supports SQLModel and provides automatic relationship detection, pagination, filtering, and more. Current version is 0.21.0, with releases every few months; Python 3.10+ required.

pip install fastcrud
error type object 'PaginatedListResponse' has no attribute '__pydantic_model__'
cause Importing PaginatedListResponse from fastcrud.paginated which was removed in v0.20.0.
fix
Change import to 'from fastcrud import PaginatedListResponse'.
error AttributeError: 'NoneType' object has no attribute 'id' (when accessing result.id after create())
cause create() without schema_to_select returns None in v0.20.0+; previously it returned the model.
fix
Either pass schema_to_select to create() or call get() after create(): result = await crud.create(db, object=item_data, schema_to_select=ItemSchema)
error ImportError: cannot import name 'FastCRUD' from 'fastcrud.crud'
cause In older installations, the import path changed; FastCRUD is now directly from fastcrud.
fix
Use 'from fastcrud import FastCRUD' instead of 'from fastcrud.crud import FastCRUD'.
breaking In v0.20.0, the create() method no longer returns the SQLAlchemy model when schema_to_select is not provided. It returns None instead, for consistency with other CRUD methods.
fix To retrieve the created model, either pass a schema_to_select or explicitly call get() after create().
breaking Python 3.9 support was dropped in v0.21.0. Minimum Python version is now 3.10.
fix Upgrade Python to 3.10 or later.
deprecated The fastcrud.paginated module is deprecated and was removed in v0.20.0. Use imports from fastcrud directly.
fix Change 'from fastcrud.paginated import PaginatedListResponse' to 'from fastcrud import PaginatedListResponse'.
gotcha The create() method triggers a deprecation warning if called without schema_to_select in v0.19.x, and returns None in v0.20+. If you rely on the returned model, you must update your code.
fix Provide schema_to_select parameter to create() or switch to get() after create.
gotcha Automatic relationship detection (include_relationships=True) is new in v0.21.0. In earlier versions, you had to manually configure joins. If upgrading, review your code that manually sets up relationships.
fix Set include_relationships=True in your routes to leverage automatic detection, or keep manual configuration.
pip install fastcrud[sqlmodel]

Minimal FastAPI app with SQLAlchemy and FastCRUD to create an item.

from fastapi import FastAPI
from pydantic import BaseModel
from sqlalchemy import Column, Integer, String, create_engine
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker
from fastcrud import FastCRUD

# Setup SQLAlchemy
Base = declarative_base()
class Item(Base):
    __tablename__ = 'items'
    id = Column(Integer, primary_key=True)
    name = Column(String)

engine = create_engine('sqlite:///./test.db')
Base.metadata.create_all(bind=engine)
SessionLocal = sessionmaker(autocommit=False, autoflush=False, bind=engine)

app = FastAPI()
crud = FastCRUD(Item)

@app.post('/items/')
async def create_item(name: str):
    db = SessionLocal()
    result = await crud.create(db=db, object={'name': name})
    db.close()
    return result