Ошибка AttributeError: 'function' object has no attribute '__mro__'
database.py
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker, DeclarativeBase
#class DataBase:
#def __init__(self,db_url):
#self.engine = create_engine(db_url)
#self.SessionLocal = sessionmaker
SQLALCHEMY_DATABASE_URL = "sqlite:///./user_date.db"
engine = create_engine(
SQLALCHEMY_DATABASE_URL,
connect_args={"check_same_thread": False})
SessionLocal = sessionmaker(autoflush=False , bind=engine)
db = SessionLocal()
class Base(DeclarativeBase):
pass
Models.py
from fastapi import File
from database import Base
from sqlalchemy.orm import Mapped, mapped_column
class ModelUser(Base):
__tablename__ = "users"
id : Mapped[int] = mapped_column(primary_key=True, index=True)
name : Mapped[str] = mapped_column()
lastname : Mapped[str] = mapped_column()
age: Mapped[int] = mapped_column()
photo: Mapped[File] = mapped_column()
main.py
from fastapi import Depends, FastAPI
from pydantic import BaseModel, Field
from database import engine, SessionLocal, Base
from models import ModelUser
from sqlalchemy.orm import Session
Base.metadata.create_all(bind=engine)
app = FastAPI()
def get_db():
db = SessionLocal()
try:
yield db
finally:
db.close()
class ModelUserDTO(BaseModel):
name : str = Field(min_length=11)
lastname: str = Field(min_length= 1, max_length=25)
age : int
photo: bytes
@app.post("/useradd/", tags=["Добавление пользователя в БД"])
# Создание Пользователя
async def CreateUser(user: ModelUserDTO, db: Session = Depends(get_db)):
db_user = ModelUser()
db_user.name = user.name
db_user.lastname = user.lastname
db_user.age = user.age
db_user.photo = user.photo
db.add(db_user)
db.commit()
return db_user
@app.get("/allusers/")
async def AllGetUsers(db: Session = Depends()):
pass
AttributeError: 'function' object has no attribute 'mro'
Просчитался но где , Напишите где я ошибся и как это исправить
Ошибка:
Process SpawnProcess-1:
Traceback (most recent call last):
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\process.py", line 314, in _bootstrap
self.run()
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\multiprocessing\process.py", line 108, in run
self._target(*self._args, **self._kwargs)
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\uvicorn\_subprocess.py", line 78, in subprocess_started
target(sockets=sockets)
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\uvicorn\server.py", line 62, in run
return asyncio.run(self.serve(sockets=sockets))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 194, in run
return runner.run(main)
^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\asyncio\runners.py", line 118, in run
return self._loop.run_until_complete(task)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\asyncio\base_events.py", line 685, in run_until_complete
return future.result()
^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\uvicorn\server.py", line 69, in serve
config.load()
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\uvicorn\config.py", line 458, in load
self.loaded_app = import_from_string(self.app)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\uvicorn\importer.py",
line 21, in import_from_string
module = importlib.import_module(module_str)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\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 995, in exec_module
File "<frozen importlib._bootstrap>", line 488, in _call_with_frames_removed
File "G:\Builds\ProjectApiPython\main.py", line 5, in <module>
from models import UserModel
File "G:\Builds\ProjectApiPython\models.py", line 7, in <module>
class ModelUser(Base):
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\decl_api.py", line 836, in __init_subclass__
_as_declarative(cls._sa_registry, cls, cls.__dict__)
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 244, in _as_declarative
return _MapperConfig.setup_mapping(registry, cls, dict_, None, {})
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 325, in setup_mapping
return _ClassScanMapperConfig(
^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 571, in __init__
self._extract_mappable_attributes()
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\decl_base.py", line 1541, in _extract_mappable_attributes
value.declarative_scan(
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\properties.py", line 709, in declarative_scan
self._init_column_for_annotation(
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\properties.py", line 866, in _init_column_for_annotation
new_sqltype = registry._resolve_type(check_type)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Users\Lamantine\AppData\Local\Programs\Python\Python312\Lib\site-packages\sqlalchemy\orm\decl_api.py", line 1259, in _resolve_type
search = ((pt, pt) for pt in python_type_type.__mro__)
^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'function' object has no attribute '__mro__'