Repository navigation
First Check
Example Code# app/main.py
from fastapi import FastAPI
from app.backend.db import init_db
init_db()
app = FastAPI()
# app/backend/db.py
from sqlmodel import Session, SQLModel, create_engine
import app.backend.models
engine = create_engine("sqlite:///dev.db")
def init_db():
SQLModel.metadata.create_all(engine)
def get_session():
with Session(engine) as session:
yield session
# tests/shared_fixtures.py
from fastapi.testclient import TestClient
import pytest
from sqlmodel import Session, SQLModel, create_engine
from app.backend.db import get_session
from app.main import app
@pytest.fixture
def session():
engine = create_engine("sqlite://")
SQLModel.metadata.create_all(engine)
with Session(engine) as session:
yield session
@pytest.fixture
def client(session):
def get_session_override():
return session
app.dependency_overrides.clear()
app.dependency_overrides[get_session] = get_session_override
with TestClient(app, follow_redirects=False) as test_client:
yield test_client
app.dependency_overrides.clear()Description
Operating SystemmacOS Operating System DetailsNo response Project Version0.0.48 Python Version3.13.6 Additional ContextNo response |
Replies: 1 comment 8 replies
|
I reproduced the duplicate-registration error by executing one model definition under two module names with SQLModel 0.0.48 and SQLAlchemy 2.1.6 on Python 3.12. That doesn't establish which case is happening in your project. Could you share the full collection traceback, the |
Okay, I found the reason
uv run python -m pytestfails at collection. While I was playing around with SQLModel a while ago, I made a file namedsqlmodel_test.pyin my project root which I did not add to git. In that file, I redefine my models, so pytest tried to reload those models.uv run python -m pytest tests/works fine. How silly of me. Thank you again for your time.