FastAPI Model error one to many relationship

I’m new to fastapi and I’m having some issues setting up a one to many relationship. My API works fine when I have the code for the relationship commented out but when trying to have the relationship in it causes an error saying

reverse_property ‘process’ on relationship ProcessModel.tasks references relationship TaskModel.process, which does not reference mapper mapped class ProcessModel->processes

I’m not sure what this means and I’ve tried looking around to find out but can’t really find any info on this. I’ve followed the basic model creation tutorial on the fastapi website which is how I built the below but am still getting the error. Any pointers would be appreciated.

from sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.orm import relationship

from database import Base

class ClientModel(Base):
    __tablename__ = "clients"

    id = Column(Integer, primary_key=True, index=True)
    client_name = Column(String, index=True)

    tasks = relationship("TaskModel", back_populates="client")


class ProcessModel(Base):
    __tablename__ = "processes"

    id = Column(Integer, primary_key=True, index=True)
    process_name = Column(String, index=True)

    tasks = relationship("TaskModel", back_populates="process")


class StatusModel(Base):
    __tablename__ = "statuses"

    id = Column(Integer, primary_key=True, index=True)
    status_name = Column(String, index=True)

    tasks = relationship("TaskModel", back_populates="status")


class TaskModel(Base):
    __tablename__ = "tasks"

    id = Column(Integer, primary_key=True, index=True)
    script_log = Column(String, index=True)
    start_timestamp = Column(DateTime, index=True)
    finish_timestamp = Column(DateTime, index=True)

    client_id = Column(Integer, ForeignKey("clients.id"))
    client = relationship("ClientModel", back_populates="tasks")

    process_id = Column(Integer, ForeignKey("processes.id"))
    process = relationship("ClientModel", back_populates="tasks")

    status_id = Column(Integer, ForeignKey("statuses.id"))
    status = relationship("ClientModel", back_populates="tasks")

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

When you add a relationship the first parameter is the object that contains the relationship to that table:

class ProcessModel(Base):
    ...

    tasks = relationship("TaskModel", back_populates="process")

This needs to match the class name given in the TaskModel relationship that points back to the ProcessModel class:

process_id = Column(Integer, ForeignKey("processes.id"))
process = relationship("ClientModel", back_populates="tasks")
                        ^---- This needs to be ProcessModel

.. since that is where we expect the relationship to be defined.

process = relationship("ProcessModel", back_populates="tasks")

You’ll have to make the same change in the two other locations where you’re still referring to ClientModel in your relationship call for other models.


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x