In general, this is a name issue for backref.
Since 1: n relationships are sometimes a bit confusing, I set the relationship attribute always on a unique site to avoid confusion.
then the name backref is always the only one. and the relationship attribute is always in the class referenced by the foreign key.
Now, to my suggestion for fixed code:
class Post(Base): last_editor_id = Column(BigInteger, ForeignKey('users.id'), nullable=True) owner_id = Column(BigInteger, ForeignKey('users.id'), nullable=False, index=True) class User(Base): '''This represents a user on the site''' __tablename__ = 'users' id = Column(BigInteger, primary_key=True, unique=True) name = Column(BigInteger, nullable=False) owned_posts = relationship('Post', backref='owner') edited_posts = relationship('Post', backref='last_editor')
Now you can get all owned User posts with User.owned_posts and all post owners with Post.owner . Same thing with last_edited attribute.
For more information, you can read the docs relationship setting
source share