Question: I know this Question has been asked a lot and believe me I checked the answers and to me my code looks fine even tough it gives error so it's not. Basically, I was trying to set up a relationship between two Entities: User and Workout. from sqlalchemy import Integer,VARCHAR,TIMESTAMP from sqlalchemy.orm import mapped_column,relationship from sqlalchemy.sql import func from app.schemas.baseschema import Base class User(Base): __tablename__="users" id=mapped_column(Integer,primary_key=True,autoincrement=True) username=mapped_column(VARCHAR(255),unique=True,nullable=False) email=mapped_column(VARCHAR(50),unique=True,nullable=False) created_at=mapped_column(TIMESTAMP(timezone=True),default=func.current_timestamp()) updated_at=mapped_column(TIMESTAMP(timezone=True)) password_hash=mapped_column(VARCHAR(255),nullable=False) workouts=relationship("Workout",back_populates="user")  from sqlalchemy import Integer,DATE,TEXT,ForeignKey from sqlalchemy.orm import mapped_column,relationship from sqlalchemy.sql import func from app.schemas.baseschema import Base from sqlalchemy.schema import ForeignKeyConstraint class Workout(Base): __tablename__="workouts" id=mapped_column(Integer,primary_key=True,autoincrement=True) date=mapped_column(DATE,default=func.current_date) notes=mapped_column(TEXT) user_id=mapped_column(Integer,ForeignKey("users.id"),nullable=False) user=relationship("User",back_populates="workouts")  the error I'm getting is this one:  InvalidRequestError("When initializing mapper Mapper[User(users)], expression 'Workout' failed to locate a name ('Workout'). If this is a class name, consider adding this relationship() to the <class 'app.schemas.userschemas.User'> class after both dependent classes have been defined.").  Can someone help me identify the issue? To me, it looks like there's the Workout class and that it has an user field. I never used sql alchemy and I'm new to Python as well. I checked both classes to see If i spelled the relationship wrong but it looked fine to me. I also tried to compare other answers given here to the same problem and tried to contextualize them to my situation but I didn't succeed

Answer: This is sort of a weird problem that I have not seen a perfect solution to. SQLAlchemy allows this "deferred" referencing of other models/etc by str name so that you don't end up with circular imports, ie. User must import Workout and Workout must import User. The problem that happens is that by not directly referencing them they might not ever be loaded/executed and do not end up in the registry and in this example sqlalchemy cannot find "Workout". Some options to mitigate this:  Put all the models in the same file, if you import User then Workout will also be executed and included in the registry because it the whole module is loaded and it exists in the same module. (This is the easiest)  """ models.py """ class User(Base): #... pass class Workout(Base): #... pass   Import all the models into a "middle" module and use models from that registry therefore forcing all the models to be loaded/registered. (Now you have to remember to do this song-and-dance every time you make a new class)  """ models/classes.py """ from .user import User from .workout import Workout #...  """ handlers.py """ # Load Workout as a side-effect. from .models.classes import User def handle_user_request(request): return to_json([u.id for u in request.db.scalars(select(User))])   Carefully import only the necessary models (this is not a great solution) Another option I don't know about