Задать вопрос
@xesax

Как решить проблему с моделями в SQLAlchemy?

получаю ошибку:
sqlalchemy.exc.InvalidRequestError: One or more mappers failed to initialize - can't proceed with initialization of other mappers. Triggering mapper: 
'Mapper[User(users)]'. Original exception was: When initializing mapper Mapper[User(users)], expression 'Times' failed to locate a name ('Times'). If 
this is a class name, consider adding this relationship() to the <class 'app.models.user_model.User'> class after both dependent classes have been defined.

вот мои модели:
from database import Base
from sqlalchemy.orm import mapped_column, Mapped, relationship
from sqlalchemy import String, DateTime, ForeignKey
from datetime import date, time, datetime
from typing import Optional, TYPE_CHECKING


if TYPE_CHECKING:
    from .user_model import User
    from .booking_model import Booking


class Times(Base):
    __tablename__ = 'times'

    id: Mapped[int] = mapped_column(primary_key=True)
    user_id: Mapped[int] = mapped_column(ForeignKey('users.id'))
    booking_id: Mapped[int] = mapped_column(ForeignKey('bookings.id'))
    time: Mapped[str]

    user: Mapped['User'] = relationship('User', back_populates='times')
    booking: Mapped['Booking'] = relationship('Booking', back_populates='times')


from database import Base
from sqlalchemy.orm import mapped_column, Mapped, relationship
from sqlalchemy import String, DateTime, ForeignKey, Date, Time, ARRAY, JSON
from datetime import date, time, datetime
from typing import Optional, TYPE_CHECKING


if TYPE_CHECKING:
    from .user_model import User
    from .time_model import Times


class Booking(Base):
    __tablename__ = 'bookings'

    id: Mapped[int] = mapped_column(primary_key=True)
    date_for_booking: Mapped[date] = mapped_column(Date, nullable=True)
    user_id: Mapped[int] = mapped_column(ForeignKey('users.id', ondelete='CASCADE'))
    times: Mapped[list[str]] = mapped_column(JSON, nullable=True)
    selected_times: Mapped[list[str]] = mapped_column(JSON, nullable=True, default=[])

    user: Mapped['User'] = relationship('User', back_populates='bookings')
    list_times: Mapped[list['Times']] = relationship('Times', back_populates='booking')


from database import Base
from sqlalchemy.orm import mapped_column, Mapped, relationship
from sqlalchemy import String, DateTime, Time, ARRAY, Date, JSON
from datetime import datetime, time, timezone
from typing import Optional, TYPE_CHECKING
from app.utils.generate_time import moscow_tz

if TYPE_CHECKING:
    from .booking_model import Booking
    from .time_model import Times


class User(Base):
    __tablename__ = 'users'

    id: Mapped[int] = mapped_column(primary_key=True)
    name: Mapped[str] = mapped_column(String(255))
    surname: Mapped[str] = mapped_column(String(255))
    profile_photo: Mapped[str] = mapped_column(String(255), default='default.jpg')
    role: Mapped[str] = mapped_column(String(255), default='user')
    email: Mapped[str] = mapped_column(String(255), unique=True)
    personal_link: Mapped[str] = mapped_column(String(255), unique=True)
    telegram_link: Mapped[str] = mapped_column(String(255), unique=True, default=None, nullable=True)
    hashed_password: Mapped[str]
    registered_at: Mapped[datetime] = mapped_column(DateTime, default=datetime.now(moscow_tz).replace(tzinfo=None))
    is_active: Mapped[bool] = mapped_column(default=True)
    description: Mapped[str | None] = mapped_column(String(500), default=None, nullable=True)
    enabled: Mapped[bool] = mapped_column(default=True)
    start_time: Mapped[time] = mapped_column(Time, default=time(7, 0))
    end_time: Mapped[time] = mapped_column(Time, default=time(20, 0))
    interval: Mapped[int] = mapped_column(default=30)
    

    bookings: Mapped[list['Booking']] = relationship('Booking', back_populates='user')
    times: Mapped[list['Times']] = relationship('Times', back_populates='user')


В чем может быть проблема?
  • Вопрос задан
  • 156 просмотров
Подписаться 1 Средний Комментировать
Пригласить эксперта
Ваш ответ на вопрос

Войдите, чтобы написать ответ

Похожие вопросы