New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Bug: SerializationException when running modeling-and-features demo from docs #3464
Closed
2 of 4 tasks
Labels
Bug 🐛
This is something that is not working as expected
Documentation 📚
This is related to documentation
Good First Issue
This is good for newcomers to take on
Comments
Can you try |
Allright, the following code, using SQLAlchemyPlugin works: from datetime import date
from typing import TYPE_CHECKING
from uuid import UUID
from sqlalchemy import ForeignKey, select
from sqlalchemy.orm import Mapped, mapped_column, relationship
from litestar import Litestar, get
from litestar.contrib.sqlalchemy.base import UUIDAuditBase, UUIDBase
from litestar.contrib.sqlalchemy.plugins import AsyncSessionConfig, SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from litestar.testing import create_test_client
if TYPE_CHECKING:
from sqlalchemy.ext.asyncio import AsyncEngine, AsyncSession
# the SQLAlchemy base includes a declarative model for you to use in your models.
# The `Base` class includes a `UUID` based primary key (`id`)
class Author(UUIDBase):
name: Mapped[str]
dob: Mapped[date]
books: Mapped[list["Book"]] = relationship(back_populates="author", lazy="selectin")
# The `AuditBase` class includes the same UUID` based primary key (`id`) and 2
# additional columns: `created_at` and `updated_at`. `created_at` is a timestamp of when the
# record created, and `updated_at` is the last time the record was modified.
class Book(UUIDAuditBase):
title: Mapped[str]
author_id: Mapped[UUID] = mapped_column(ForeignKey("author.id"))
author: Mapped[Author] = relationship(lazy="joined", innerjoin=True, viewonly=True)
session_config = AsyncSessionConfig(expire_on_commit=False)
sqlalchemy_config = SQLAlchemyAsyncConfig(
connection_string="sqlite+aiosqlite:///testb.sqlite", session_config=session_config
) # Create 'async_session' dependency.
sqlalchemy_plugin = SQLAlchemyPlugin(config=sqlalchemy_config)
async def on_startup() -> None:
"""Initializes the database."""
async with sqlalchemy_config.get_engine().begin() as conn:
await conn.run_sync(UUIDBase.metadata.create_all)
async with sqlalchemy_config.get_session() as session:
session.add(Author(name="author1",dob=date(1990,1,1),id=UUID("00000000-0000-0000-0000-000000000001") ))
session.add(Book(title="blaat",author_id=UUID("00000000-0000-0000-0000-000000000001")))
await session.commit()
@get(path="/authors")
async def get_authors(db_session: "AsyncSession", db_engine: "AsyncEngine") -> list[Author]:
"""Interact with SQLAlchemy engine and session."""
return list(await db_session.scalars(select(Author)))
app = Litestar(
route_handlers=[get_authors],
on_startup=[on_startup],
plugins=[SQLAlchemyPlugin(config=sqlalchemy_config)],
debug=True
)
if __name__ == "__main__":
import uvicorn
uvicorn.run(app, host="0.0.0.0", port=8001, log_level="debug") A change in the documentation would be nice |
@JorenSix Feel free to PR if you want to, if not someone else will pick this up as per their availability |
JorenSix
added a commit
to GhentCDH/litestar
that referenced
this issue
May 6, 2024
Now uses the SQLAlchemyPlugin in stead of SQLAlchemyInitPlugin, see litestar-org#3464
This was referenced May 6, 2024
peterschutt
added a commit
that referenced
this issue
May 17, 2024
…eption for docs (#3475) * Update sqlalchemy_declarative_models.py Now uses the SQLAlchemyPlugin in stead of SQLAlchemyInitPlugin, see #3464 * Update sqlalchemy_declarative_models.py Ruff lint fixes * Update docs/examples/contrib/sqlalchemy/sqlalchemy_declarative_models.py create_all flag Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com> * Update docs/examples/contrib/sqlalchemy/sqlalchemy_declarative_models.py Co-authored-by: Peter Schutt <peter.github@proton.me> * Update docs/examples/contrib/sqlalchemy/sqlalchemy_declarative_models.py Further documentation Co-authored-by: Peter Schutt <peter.github@proton.me> * Update docs/examples/contrib/sqlalchemy/sqlalchemy_declarative_models.py Co-authored-by: Peter Schutt <peter.github@proton.me> * Update docs/examples/contrib/sqlalchemy/sqlalchemy_declarative_models.py Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com> * Unit test for sqlalchemy_declarative_models. * Now using option create_all=True for meta-data, on_startup for dummy data * Lint ruff-format compatibility * Fix for 3.9 and 3.10 wrt class declarations/typing * Ruff format check * Removed comments which might be heavy for example code * For 3.8 compatibility wrt typing now using List in stead of list * Replaced incorrect use of List instead of List --------- Co-authored-by: Cody Fincher <204685+cofin@users.noreply.github.com> Co-authored-by: Peter Schutt <peter.github@proton.me>
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Labels
Bug 🐛
This is something that is not working as expected
Documentation 📚
This is related to documentation
Good First Issue
This is good for newcomers to take on
Description
Hi,
First of all thanks for developing Litestar, it proves to be a very useful piece of software here. Unfortunately I ran into an issue.
I ran into an
msgspec_error
when requesting a page backed by sqlalchemy models which are connected via relationships. It seems that the database is correctly queried, a list of objects are returned, but then an exception is thrown when converting the objects to JSON.I ran into this issue on my production code but when isolating an MCVE I noticed that the provided example in the documentation also shows the same unexpected behaviour on tested on two different machines. One crucial change to the code is however adding an author to the database.
Since this is quite a show-stopper for me: Thanks in advance for having a look at this!
URL to code causing the issue
https://docs.litestar.dev/2/tutorials/repository-tutorial/01-modeling-and-features.html
MCVE
Steps to reproduce
Screenshots
No response
Logs
Litestar Version
2.8.2
Platform
Note
While we are open for sponsoring on GitHub Sponsors and
OpenCollective, we also utilize Polar.sh to engage in pledge-based sponsorship.
Check out all issues funded or available for funding on our Polar.sh dashboard
The text was updated successfully, but these errors were encountered: