I get this error: I don’t understand how to solve it,
I get this error when I run uvicorn
to exploit the error of cyclic model imports, I used if TYPE_CHEKING:
sqlalchemy.ext.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 bath dependent classes have been defined.
here are my models:
Times
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')
User
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')
Booking
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')
I tried to remove the Mapped, as well as the Model in the relationship
Deppy is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.