Home > OS >  FastAPI Model error one to many relationship
FastAPI Model error one to many relationship

Time:05-01

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")

CodePudding user response:

After changing the foreign key field on each of the fields in the TaskModel this sorted the issue

CodePudding user response:

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.

  • Related