Coding-Crashkurse / FastAPI-Auth

14 stars 5 forks source link

<class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type #3

Open DieSchoeneWolke opened 8 months ago

DieSchoeneWolke commented 8 months ago

Hallo ich komme leider nicht weiter ab 28:40, da ich folgende Fehlermeldung erhalte:

(.venv) PS C:\Users\coder\PycharmProjects\FastAPIYT2> uvicorn app.main:app --reload
INFO:     Will watch for changes in these directories: ['C:\\Users\\coder\\PycharmProjects\\FastAPIYT2']
INFO:     Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit)
INFO:     Started reloader process [17344] using WatchFiles              
Process SpawnProcess-1:
Traceback (most recent call last):                                                                                             
  File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\process.py", line 314, in _bootstrap          
    self.run()                                                                                                                 
  File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\process.py", line 108, in run                 
    self._target(*self._args, **self._kwargs)                                                                                  
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\uvicorn\_subprocess.py", line 78, in subprocess_started
    target(sockets=sockets)                                                                                                    
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\uvicorn\server.py", line 62, in run
    return asyncio.run(self.serve(sockets=sockets))
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
    return runner.run(main)
           ^^^^^^^^^^^^^^^^
  File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
    return self._loop.run_until_complete(task)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 684, in run_until_complete
    return future.result()
           ^^^^^^^^^^^^^^^
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\uvicorn\server.py", line 69, in serve
    config.load()
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\uvicorn\config.py", line 458, in load
    self.loaded_app = import_from_string(self.app)
                      ^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string
    module = importlib.import_module(module_str)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\importlib\__init__.py", line 90, in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "<frozen importlib._bootstrap>", line 1387, in _gcd_import
  File "<frozen importlib._bootstrap>", line 1360, in _find_and_load
  File "<frozen importlib._bootstrap>", line 1331, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 935, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 994, in exec_module
  File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\app\main.py", line 3, in <module>
    from . import database, models_and_schemas, crud
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\app\models_and_schemas.py", line 17, in <module>
    class User(BaseUser, table=True):
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\sqlmodel\main.py", line 474, in __new__
    col = get_column_from_field(v)
          ^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\sqlmodel\main.py", line 618, in get_column_from_field
    sa_type = get_sqlalchemy_type(field)
              ^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\Lib\site-packages\sqlmodel\main.py", line 607, in get_sqlalchemy_type
    raise ValueError(f"{type_} has no matching SQLAlchemy type")
ValueError: <class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type

Der Code bis dato befindet sich in diesem Repo: https://github.com/DieSchoeneWolke/FastAPIYT2

Vielen Dank für die Hilfe!

Coding-Crashkurse commented 8 months ago

Hi Jan,

Das:

class UserModel(SQLModel): email: EmailStr

zu: class UserModel(SQLModel): email: EmailStr = Field(sa_type=AutoString)

ist seit der neuesten Pydantic Version notwendig. VG, Markus

Am Mi., 24. Jan. 2024 um 14:24 Uhr schrieb Jan Schönfeld < @.***>:

Hallo ich komme leider nicht weiter ab 28:40, da ich folgende Fehlermeldung erhalte:

(.venv) PS C:\Users\coder\PycharmProjects\FastAPIYT2> uvicorn app.main:app --reload INFO: Will watch for changes in these directories: ['C:\Users\coder\PycharmProjects\FastAPIYT2'] INFO: Uvicorn running on http://127.0.0.1:8000 (Press CTRL+C to quit) INFO: Started reloader process [17344] using WatchFiles Process SpawnProcess-1: Traceback (most recent call last): File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\process.py", line 314, in _bootstrap self.run() File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\process.py", line 108, in run self._target(*self._args, **self._kwargs) File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\uvicorn_subprocess.py", line 78, in subprocess_started target(sockets=sockets) File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\uvicorn\server.py", line 62, in run return asyncio.run(self.serve(sockets=sockets)) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run return runner.run(main) ^^^^^^^^^^^^^^^^ File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run return self._loop.run_until_complete(task) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 684, in run_until_complete return future.result() ^^^^^^^^^^^^^^^ File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\uvicorn\server.py", line 69, in serve config.load() File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\uvicorn\config.py", line 458, in load self.loaded_app = import_from_string(self.app) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\uvicorn\importer.py", line 21, in import_from_string module = importlib.import_module(module_str) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\coder\AppData\Local\Programs\Python\Python312\Lib\importlib__init__.py", line 90, in import_module return _bootstrap._gcd_import(name[level:], package, level) ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ File "", line 1387, in _gcd_import File "", line 1360, in _find_and_load File "", line 1331, in _find_and_load_unlocked File "", line 935, in _load_unlocked File "", line 994, in exec_module File "", line 488, in _call_with_frames_removed File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\app\main.py", line 3, in from . import database, models_and_schemas, crud File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\app\models_and_schemas.py", line 17, in class User(BaseUser, table=True): File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\sqlmodel\main.py", line 474, in new col = get_column_from_field(v) ^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\sqlmodel\main.py", line 618, in get_column_from_field sa_type = get_sqlalchemy_type(field) ^^^^^^^^^^^^^^^^^^^^^^^^^^ File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\Lib\site-packages\sqlmodel\main.py", line 607, in get_sqlalchemytype raise ValueError(f"{type} has no matching SQLAlchemy type") ValueError: <class 'pydantic.networks.EmailStr'> has no matching SQLAlchemy type

Der Code bis dato befindet sich in diesem Repo: https://github.com/DieSchoeneWolke/FastAPIYT2

Vielen Dank für die Hilfe!

— Reply to this email directly, view it on GitHub https://github.com/Coding-Crashkurse/FastAPI-Auth/issues/3, or unsubscribe https://github.com/notifications/unsubscribe-auth/APLB3U2FCHJQYZFUXSDZEFDYQEDPBAVCNFSM6AAAAABCIYFUGOVHI2DSMVQWIX3LMV43ASLTON2WKOZSGA4TQMRWGEZTINY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

DieSchoeneWolke commented 8 months ago

Vielen Dank für die schnelle Antwort Markus! Leider komme ich dennoch dadurch nicht weiter.

Aus dem YT Video geht folgender Code mit der Änderung hervor:

from sqlmodel import SQLModel, Field
from pydantic import EmailStr
from enum import Enum
from typing import Optional

class Roles(str, Enum):
    user = "user"
    admin = "admin"

class BaseUser(SQLModel):
    email: EmailStr = Field(sa_type=AutoString)
    username: str
    is_active: bool = False
    role: Roles

class User(BaseUser, table=True):
    id: Optional[int] = Field(default=None, primary_key=True)
    hashed_password: str

class UserSchema(BaseUser):
    password: str

Als Fehlermeldung erhalte ich nun:

  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\app\main.py", line 3, in <module>
    from . import database, models_and_schemas, crud
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\app\models_and_schemas.py", line 11, in <module>
    class BaseUser(SQLModel):
  File "C:\Users\coder\PycharmProjects\FastAPIYT2\.venv\app\models_and_schemas.py", line 12, in BaseUser
    email: EmailStr = Field(sa_type=AutoString)
                                    ^^^^^^^^^^
NameError: name 'AutoString' is not defined
Coding-Crashkurse commented 8 months ago

from sqlmodel import Field, Relationship, SQLModel, AutoString

importieren musst du es schon >_<

Am Mi., 24. Jan. 2024 um 15:03 Uhr schrieb Jan Schönfeld < @.***>:

Vielen Dank für die schnelle Antwort Markus! Leider komme ich dennoch dadurch nicht weiter.

Aus dem YT Video geht folgender Code mit der Änderung hervor:

from sqlmodel import SQLModel, Field from pydantic import EmailStr from enum import Enum from typing import Optional

class Roles(str, Enum): user = "user" admin = "admin"

class BaseUser(SQLModel): email: EmailStr = Field(sa_type=AutoString) username: str is_active: bool = False role: Roles

class User(BaseUser, table=True): id: Optional[int] = Field(default=None, primary_key=True) hashed_password: str

class UserSchema(BaseUser): password: str

Als Fehlermeldung erhalte ich nun:

File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\app\main.py", line 3, in from . import database, models_and_schemas, crud File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\app\models_and_schemas.py", line 11, in class BaseUser(SQLModel): File "C:\Users\coder\PycharmProjects\FastAPIYT2.venv\app\models_and_schemas.py", line 12, in BaseUser email: EmailStr = Field(sa_type=AutoString) ^^^^^^^^^^ NameError: name 'AutoString' is not defined

— Reply to this email directly, view it on GitHub https://github.com/Coding-Crashkurse/FastAPI-Auth/issues/3#issuecomment-1908184411, or unsubscribe https://github.com/notifications/unsubscribe-auth/APLB3U6O7ZTW6C7KRGLJ7VDYQEIAPAVCNFSM6AAAAABCIYFUGOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBYGE4DINBRGE . You are receiving this because you commented.Message ID: @.***>

DieSchoeneWolke commented 8 months ago

Vielen Dank, jetzt hat es geklappt! Dein Channel ist echt super!

Coding-Crashkurse commented 8 months ago

Danke und gerne!

Jan Schönfeld @.***> schrieb am Mi., 24. Jan. 2024, 18:12:

Vielen Dank, jetzt hat es geklappt! Dein Channel ist echt super!

— Reply to this email directly, view it on GitHub https://github.com/Coding-Crashkurse/FastAPI-Auth/issues/3#issuecomment-1908573946, or unsubscribe https://github.com/notifications/unsubscribe-auth/APLB3U7VASRKKSRFV75TMWDYQE6HTAVCNFSM6AAAAABCIYFUGOVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTSMBYGU3TGOJUGY . You are receiving this because you commented.Message ID: @.***>