Это модели моего профиля и пользователя:
Код: Выделить всё
class User(Base):
__tablename__ = 'users'
id = Column("id", Integer, primary_key=True, autoincrement=True)
email = Column("email", String, unique=True, index=True)
password = Column("password", String)
created_at = Column("created_at", DateTime, default=func.now())
class Profile(Base):
__tablename__ = 'profiles'
id = Column("id", Integer, primary_key=True, autoincrement=True)
user_id = Column("user_id", Integer, ForeignKey("users.id"))
first_name = Column("first_name", String, nullable=True)
last_name = Column("last_name", String, nullable=True)
bio = Column("bio", String, nullable=True)
created_at = Column("created_at", DateTime, default=func.now())
Код: Выделить всё
class UserBase(BaseModel):
email: str
class UserCreate(UserBase):
password: str
class User(UserBase):
id: int
created_at: datetime
class Config:
from_attributes = True
class Profile(BaseModel):
user_id: int
first_name: Optional[str]
last_name: Optional[str]
bio: Optional[str]
class Config:
from_attributes = True
Код: Выделить всё
@router.post("/register", response_model=schemas.User)
async def register(usert: schemas.UserCreate, db: AsyncSession = Depends(get_db)):
try:
new_user = await user.create_user(db, usert)
if(new_user):
await profile.create_profile(db, new_user.id)
except CustomException as e:
raise HTTPException(status_code=e.status_code, detail=e.message)
except Exception as e:
raise HTTPException(status_code=400, detail=e)
return new_user
Код: Выделить всё
async def create_user(db: AsyncSession, user: UserCreate):
db_user = await get_user_by_email(db, user.email)
if(db_user):
raise EmailAlreadyRegisteredError()
hashed_password = hash_password(user.password)
new_user = User(email=user.email, password=hashed_password)
db.add(new_user)
try:
await db.commit()
await db.refresh(new_user)
return new_user
except Exception:
await db.rollback()
raise CreatingUserError()
async def get_user_by_email(db: AsyncSession, email: str):
result = await db.execute(select(User).filter(User.email == email))
return result.scalar_one_or_none()
async def create_profile(db: AsyncSession, user_id: int):
new_profile = Profile(user_id=user_id)
db.add(new_profile)
try:
await db.commit()
await db.refresh(new_profile)
return new_profile
except Exception:
await db.rollback()
raise CreatingProfileError()
Код: Выделить всё
fastapi.exceptions.ResponseValidationError: 3 validation errors:
{'type': 'get_attribute_error', 'loc': ('response', 'email'), 'msg': "Error extracting attribute: MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)", 'input': , 'ctx': {'error': "MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)"}}
{'type': 'get_attribute_error', 'loc': ('response', 'id'), 'msg': "Error extracting attribute: MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)", 'input': , 'ctx': {'error': "MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)"}}
{'type': 'get_attribute_error', 'loc': ('response', 'created_at'), 'msg': "Error extracting attribute: MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)", 'input': , 'ctx': {'error': "MissingGreenlet: greenlet_spawn has not been called; can't call await_only() here. Was IO attempted in an unexpected place? (Background on this error at: https://sqlalche.me/e/20/xd2s)"}}
Кстати, пользователь и профиль создается.
Подробнее здесь: https://stackoverflow.com/questions/787 ... sqlalchemy