SQLModel error when autogenerating Alembic revisions

253 Views Asked by At

I have a repository.models.models file:

from datetime import date
from typing import Type, Optional, List
from sqlmodel import SQLModel, Field, Relationship


class Compra(SQLModel, table=True):
    com_id: Optional[int] = Field(default=None, primary_key=True)
    data: Type[date]
    item: str
    valor: float
    forn_id: Optional[int] = Field(default=None, foreign_key="fornecedor.forn_id")

    fornecedor: Optional["Fornecedor"] = Relationship(back_populates="compras")


class Fornecedor(SQLModel, table=True):
    forn_id: Optional[int] = Field(default=None, primary_key=True)
    nome: str
    rating: int = 5

    compras: List[Compra] = Relationship(back_populates="fornecedores")

I'm using Alembic to autogenerate my revisions. When I do a alembic revision --autogenerate -m "Create models" I get this traceback:

Traceback (most recent call last):
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/bin/alembic", line 8, in <
module>
    sys.exit(main())
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/config.py", line 590, in main
    CommandLine(prog=prog).main(argv=argv)
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/config.py", line 584, in main
    self.run_cmd(cfg, options)
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/config.py", line 561, in run_cmd
    fn(
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/command.py", line 229, in revision
    script_directory.run_env()
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/script/base.py", line 569, in run_env
    util.load_python_file(self.dir, "env.py")
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/util/pyfiles.py", line 94, in load_python_file
    module = load_module_py(module_id, path)
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/alembic/util/pyfiles.py", line 110, in load_module_py
    spec.loader.exec_module(module)  # type: ignore
  File "<frozen importlib._bootstrap_external>", line 883, in exec_module
  File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
  File "/home/andre/Projects/rest_api_for_bizagi/migrations/env.py", line 9, in <module>
    from repository.models import Fornecedor, Compra  # noqa
  File "/home/andre/Projects/rest_api_for_bizagi/./repository/models/__init__.py", line 1, in <module>
    from .models import Fornecedor  # noqa
  File "/home/andre/Projects/rest_api_for_bizagi/./repository/models/models.py", line 6, in <module>
    class Compra(SQLModel, table=True):
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/sqlmodel/main.py", line 293, in __new__
    col = get_column_from_field(v)
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/sqlmodel/main.py", line 421, in get_column_from_field
    sa_type = get_sqlachemy_type(field)
  File "/home/andre/.cache/pypoetry/virtualenvs/rest-api-for-bizagi-9TxUSah3-py3.10/lib/python3.10/site-packag
es/sqlmodel/main.py", line 375, in get_sqlachemy_type
    if issubclass(field.type_, str):
TypeError: issubclass() arg 1 must be a class

I'm pretty sure that Alembic is configured correctly and can connect with database (as the first blank revision indeed update this database). If I ommit the table=True option it gives me a blank revision.

What could be the error here?

1

There are 1 best solutions below

0
On

For anyone that stumbles here:

The problem was the Type[date] typing. I just removed the Type and everything worked fine.

from datetime import date
from typing import Optional, List
from sqlmodel import SQLModel, Field, Relationship


class Compra(SQLModel, table=True):
    com_id: Optional[int] = Field(default=None, primary_key=True)
    data: date # that line was the problem
    item: str
    valor: float
    forn_id: Optional[int] = Field(default=None, foreign_key="fornecedor.forn_id")

    fornecedor: Optional["Fornecedor"] = Relationship(back_populates="compras")


class Fornecedor(SQLModel, table=True):
    forn_id: Optional[int] = Field(default=None, primary_key=True)
    nome: str
    rating: int = 5

    compras: List[Compra] = Relationship(back_populates="fornecedores")