Skip to content

I get type error if I use __root__ from pydantic while inheriting from SQLModelΒ #281

@jd-solanki

Description

@jd-solanki

First Check

  • I added a very descriptive title to this issue.
  • I used the GitHub search to find a similar issue and didn't find it.
  • I searched the SQLModel documentation, with the integrated search.
  • I already searched in Google "How to X in SQLModel" and didn't find any information.
  • I already read and followed all the tutorial in the docs and didn't find an answer.
  • I already checked if it is not related to SQLModel but to Pydantic.
  • I already checked if it is not related to SQLModel but to SQLAlchemy.

Commit to Help

  • I commit to help with one of those options πŸ‘†

Example Code

from sqlmodel import SQLModel
from pydantic import BaseModel

data = [
    { "id": 1, "name": "awesome-product" }
]

class ProductBase(SQLModel):
    name: str

class ProductOut(ProductBase):
    id: int

# Here πŸ‘‹  If I inherit from `SQLModel`` then I get type error. However, If I inherit from `BaseModel` then I don't get error.
# UnComment below line and comment the `SQLModel` usage to resolve the type error
# class ProductList(BaseModel):
class ProductList(SQLModel):
    __root__: list[ProductOut]

class SomeResponse(SQLModel):
    products: ProductList
    msg: str

product_list_model = ProductList.parse_obj(data)
SomeResponse(products=product_list_model, msg="Hello world")

Description

I get a type error if I inherit ProductList model from SQLModel saying:

Argument of type "SQLModel" cannot be assigned to parameter "products" of type "ProductList" in function "__init__"
  "SQLModel" is incompatible with "ProductList"

However, If I use BaseModel from pydantic for inheritance error went away.

Below line gives type error

class ProductList(SQLModel):

Below line looks fine

class ProductList(BaseModel):

Operating System

Linux

Operating System Details

Ubuntu 21.10

SQLModel Version

0.0.6

Python Version

3.10.2

Additional Context

image

Metadata

Metadata

Assignees

No one assigned

    Labels

    questionFurther information is requested

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions