Skip to content
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: Litestar: "Unexpected default value" exception when using default=null() from sqlalchemy #238

Open
1 of 4 tasks
aquirdTurtle opened this issue Jul 26, 2024 · 4 comments
Labels
wontfix This will not be worked on

Comments

@aquirdTurtle
Copy link

Description

There is a null() function in sqlalchemy which is useful on JSON objects to disambiguate sql null vs json null as a value. The advanced alchemy package appears to not have support for this however. Running the code below results in an unexpected exception:

  File "/home/<>/miniconda3/envs/testenv/lib/python3.11/site-packages/advanced_alchemy/extensions/litestar/dto.py", line 311, in _detect_defaults
    raise ValueError(msg)
ValueError: Unexpected default type

Examining the sqla_default object further reveals that the default _detect_defaults retrieves:

sqla_default=ColumnElementColumnDefault(<sqlalchemy.sql.elements.Null object at 0x790f30be0d50>), sqla_default.is_identity=False, sqla_default.is_clause_element=True, sqla_default.has_arg=True

I'm not familiar enough with this ecosystem to be confident in any fixes, I'm not sure if just detecting the above and setting the default=None would work though?

URL to code causing the issue

No response

MCVE

from __future__ import annotations

import traceback
from typing import Any

from advanced_alchemy.base import UUIDBase
from litestar import Litestar, get
from litestar.contrib.sqlalchemy.plugins import SQLAlchemyAsyncConfig, SQLAlchemyPlugin
from sqlalchemy import null, select
from sqlalchemy import types as sql_types
from sqlalchemy.ext.asyncio import AsyncSession
from sqlalchemy.orm import Mapped, mapped_column

connection_string = "sqlite+aiosqlite:////tmp/test1.sqlite"
sqlalchemy_config = SQLAlchemyAsyncConfig(create_all=True, connection_string=connection_string)
sqlalchemy_plugin = SQLAlchemyPlugin(config=sqlalchemy_config)


class Foo(UUIDBase):
    __tablename__ = "foo"
    value_json: Mapped[Any] = mapped_column(sql_types.JSON, default=null())


@get("/get_foo")
async def get_foo(db_session: AsyncSession) -> list[Foo]:
    resp = await db_session.scalars(select(Foo))
    return resp.all()


def plain_text_exception_handler(_, exc):
    print(traceback.format_exc())


app = Litestar(
    route_handlers=[get_foo],
    plugins=[sqlalchemy_plugin],
    exception_handlers={Exception: plain_text_exception_handler},
)

Steps to reproduce

0. conda env create -n testenv
1. pip install litestar, sqlalchemy, advanced_alchemy
2. uvicorn testfile:app
3. Observe error

Screenshots

"In the format of: ![SCREENSHOT_DESCRIPTION](SCREENSHOT_LINK.png)"

Logs

File "/home/<>/miniconda3/envs/testenv/lib/python3.11/site-packages/advanced_alchemy/extensions/litestar/dto.py", line 311, in _detect_defaults
    raise ValueError(msg)
ValueError: Unexpected default type

Package Version

advanced-alchemy is version 0.17.3. Full list:

$ conda list
# Name                    Version                   Build  Channel
_libgcc_mutex             0.1                 conda_forge    conda-forge
_openmp_mutex             4.5                       2_gnu    conda-forge
advanced-alchemy          0.17.3                   pypi_0    pypi
alembic                   1.13.2                   pypi_0    pypi
anyio                     4.4.0                    pypi_0    pypi
argcomplete               3.4.0              pyhd8ed1ab_0    conda-forge
bzip2                     1.0.8                h4bc722e_7    conda-forge
ca-certificates           2024.7.4             hbcca054_0    conda-forge
certifi                   2024.7.4                 pypi_0    pypi
click                     8.1.7           unix_pyh707e725_0    conda-forge
colorama                  0.4.6              pyhd8ed1ab_0    conda-forge
faker                     26.0.0                   pypi_0    pypi
go-sops                   3.7.3                hfdd54a8_0    conda-forge
greenlet                  3.0.3                    pypi_0    pypi
h11                       0.14.0                   pypi_0    pypi
httpcore                  1.0.5                    pypi_0    pypi
httpx                     0.27.0                   pypi_0    pypi
icu                       73.2                 h59595ed_0    conda-forge
idna                      3.7                      pypi_0    pypi
ld_impl_linux-64          2.40                 hf3520f5_7    conda-forge
libexpat                  2.6.2                h59595ed_0    conda-forge
libffi                    3.4.2                h7f98852_5    conda-forge
libgcc-ng                 14.1.0               h77fa898_0    conda-forge
libgomp                   14.1.0               h77fa898_0    conda-forge
libnsl                    2.0.1                hd590300_0    conda-forge
libsqlite                 3.46.0               hde9e2c9_0    conda-forge
libstdcxx-ng              14.1.0               hc0a3c3a_0    conda-forge
libuuid                   2.38.1               h0b41bf4_0    conda-forge
libuv                     1.48.0               hd590300_0    conda-forge
libxcrypt                 4.4.36               hd590300_1    conda-forge
libzlib                   1.3.1                h4ab18f5_1    conda-forge
litestar                  2.10.0                   pypi_0    pypi
mako                      1.3.5                    pypi_0    pypi
markdown-it-py            3.0.0                    pypi_0    pypi
markupsafe                2.1.5                    pypi_0    pypi
mdurl                     0.1.2                    pypi_0    pypi
msgspec                   0.18.6                   pypi_0    pypi
multidict                 6.0.5                    pypi_0    pypi
ncurses                   6.5                  h59595ed_0    conda-forge
nodejs                    20.9.0               hb753e55_1    conda-forge
openssl                   3.3.1                h4bc722e_2    conda-forge
packaging                 24.1               pyhd8ed1ab_0    conda-forge
pip                       24.0               pyhd8ed1ab_0    conda-forge
pipx                      1.5.0              pyhd8ed1ab_0    conda-forge
platformdirs              4.2.2              pyhd8ed1ab_0    conda-forge
polyfactory               2.16.2                   pypi_0    pypi
pygments                  2.18.0                   pypi_0    pypi
python                    3.11.9          hb806964_0_cpython    conda-forge
python-dateutil           2.9.0.post0              pypi_0    pypi
pyyaml                    6.0.1                    pypi_0    pypi
readline                  8.2                  h8228510_1    conda-forge
rich                      13.7.1                   pypi_0    pypi
rich-click                1.8.3                    pypi_0    pypi
setuptools                71.0.4             pyhd8ed1ab_0    conda-forge
six                       1.16.0                   pypi_0    pypi
sniffio                   1.3.1                    pypi_0    pypi
sqlalchemy                2.0.31                   pypi_0    pypi
tk                        8.6.13          noxft_h4845f30_101    conda-forge
tomli                     2.0.1              pyhd8ed1ab_0    conda-forge
typing-extensions         4.12.2                   pypi_0    pypi
tzdata                    2024a                h0c530f3_0    conda-forge
userpath                  1.7.0              pyhd8ed1ab_0    conda-forge
uvicorn                   0.30.3                   pypi_0    pypi
wheel                     0.43.0             pyhd8ed1ab_1    conda-forge
xz                        5.2.6                h166bdaf_0    conda-forge
yarn                      1.22.19              ha770c72_0    conda-forge
zlib                      1.3.1                h4ab18f5_1    conda-forge

Platform

  • Linux
  • Mac
  • Windows
  • Other (Please specify in the description above)
@aquirdTurtle aquirdTurtle added the bug Something isn't working label Jul 26, 2024
@aquirdTurtle aquirdTurtle changed the title Bug: Unexpected default value when using default=null() from sqlalchemy Bug: Litestar: Unexpected default value when using default=null() from sqlalchemy Jul 26, 2024
@aquirdTurtle aquirdTurtle changed the title Bug: Litestar: Unexpected default value when using default=null() from sqlalchemy Bug: Litestar: "Unexpected default value" exception when using default=null() from sqlalchemy Jul 26, 2024
@cofin
Copy link
Member

cofin commented Aug 5, 2024

can you confirm the data type you were using when you say this? I suspect it was not a string or int type, maybe something like a datetime?

@aquirdTurtle
Copy link
Author

The column type is json (an example where the null() function is useful) and the app crashes before anything is added to the Foo table. Or did you mean something else?

@cofin
Copy link
Member

cofin commented Aug 21, 2024

Can you try using the server_default instead of default for these types of default value configurations? It's currently not possible to use the DTO to infer these types of defaults.

@cofin cofin added wontfix This will not be worked on and removed bug Something isn't working labels Aug 21, 2024
@Mero89
Copy link

Mero89 commented Aug 24, 2024

Hello @aquirdTurtle i think that the problem lies in the definition of the sqlalchemy Model.
The null() expression renders to “SQL NULL”, while you might want to use JSON.NULL.
look at this note:
https://docs.sqlalchemy.org/en/21/core/type_basics.html#sqlalchemy.types.JSON.NULL

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
wontfix This will not be worked on
Projects
None yet
Development

No branches or pull requests

3 participants