Skip to content

ValueError: <class 'dict'> has no matching SQLAlchemy type #1308

Closed Answered by YuriiMotov
Soulbadguy54 asked this question in Questions
Discussion options

You must be logged in to vote

The problem is in the import:

from pydantic import BaseModel, Field, field_validator

You should import Field from sqlmodel, not from pydantic

from typing import Any, Optional

from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.ext.mutable import MutableDict
from sqlmodel import Field, Session, SQLModel, create_engine


class Combo(SQLModel, table=True):
    id: Optional[int] = Field(primary_key=True)
    data: dict[str, Any] = Field(
        default_factory=dict,
        sa_type=MutableDict.as_mutable(JSONB),
    )


engine = create_engine(
    "postgresql://user:mysecretpassword@localhost/some_db", echo=True
)
SQLModel.metadata.drop_all(engine)
SQLModel.metadata.create_all(e…

Replies: 2 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by YuriiMotov
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
3 participants