sqlmodel icon indicating copy to clipboard operation
sqlmodel copied to clipboard

♻️ Ignore `on_event is deprecated` deprecation warnings, fix `ValidationError` import warning

Open YuriiMotov opened this issue 3 months ago • 2 comments

Currently we have a lot of deprecation warnings because we use fastapi.on_event in docs examples. These warnings are useless and make it hard to read log of tests. I suggest we just ignore "on_event is deprecated" warnings globally.

I also fixed import of ValidationError which also produced a deprecation warning.

With Pydantic V2 we still have 1 type of warnings:

SAWarning: Object of type <HeroTeamLink> not in session, add operation along 'Hero.team_links' will not proceed (This warning originated from the Session 'autoflush' process, which was invoked automatically in response to a user-initiated operation. Consider using ``no_autoflush`` context manager if this warning happened while initializing objects.)

Not sure how to fix this one..

See log
tests/test_tutorial/test_many_to_many/test_tutorial003.py::test_tutorial
tests/test_tutorial/test_many_to_many/test_tutorial003_py310.py::test_tutorial
tests/test_tutorial/test_many_to_many/test_tutorial003_py39.py::test_tutorial
  /home/yurii/code/sqlmodel_forks/motov/sqlmodel/sqlmodel/orm/session.py:144: SAWarning: Object of type <HeroTeamLink> not in session, add operation along 'Hero.team_links' will not proceed (This warning originated from the Session 'autoflush' process, which was invoked automatically in response to a user-initiated operation. Consider using ``no_autoflush`` context manager if this warning happened while initializing objects.)
    return super().execute(

tests/test_tutorial/test_many_to_many/test_tutorial003.py::test_tutorial
tests/test_tutorial/test_many_to_many/test_tutorial003_py310.py::test_tutorial
tests/test_tutorial/test_many_to_many/test_tutorial003_py39.py::test_tutorial
  /home/yurii/code/sqlmodel_forks/motov/sqlmodel/sqlmodel/orm/session.py:144: SAWarning: Object of type <HeroTeamLink> not in session, add operation along 'Team.hero_links' will not proceed (This warning originated from the Session 'autoflush' process, which was invoked automatically in response to a user-initiated operation. Consider using ``no_autoflush`` context manager if this warning happened while initializing objects.)
    return super().execute(

With Pydantic V1 we still have a lot of warnings, but they are not easily fixable. Anyway, with this PR we will have less warnings and it will be easier to review test logs

YuriiMotov avatar Oct 09 '25 21:10 YuriiMotov

📝 Docs preview

Last commit 7c8a3308d1ccf4c4e5c6b97ac44471532e8f4bec at: https://4709c771.sqlmodel.pages.dev

github-actions[bot] avatar Oct 09 '25 21:10 github-actions[bot]

Maybe I've forgotten/missed some previous discussion about this, but why don't we update the usage of on_event, e.g. like #958 attempts to do?

#958 uses blocking function inside async lifespan function. That's not a good practice..

@asynccontextmanager
async def lifespan(app: FastAPI):
    create_db_and_tables()
    yield

We can update it to run it in the threadpool, but it will make the code example more complicated..

YuriiMotov avatar Oct 21 '25 14:10 YuriiMotov