I have the following two models in several files:
routes/camera/model.py
from services.db import db
from dataclasses import dataclass
from routes.project.models import Project
@dataclass
class Camera(db.Model):
__tablename__ = 'cameras'
id: int
name: str
project_id: int
url: str
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
url = db.Column(db.String(1000), unique=True, nullable=False)
project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), nullable=False)
db.create_all()
routes/project/model.py
from services.db import db
from dataclasses import dataclass
from routes.camera.models import Camera
@dataclass
class Project(db.Model):
__tablename__ = 'projects'
id: int
name: str
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(80), unique=True, nullable=False)
cameras = db.relationship(Camera, backref='projects')
db.create_all()
The problem is that I need make a insert by project name in camera in order to do this I have the following approach:
def create_camera(name: str, url:str, project_name:str):
project = Project(name=project_name)
camera = Camera(name=name, project_id=project, url=url)
db.session.add(camera)
db.session.commit()
but I get a circular dependant and I cannot make this insert
So, how can I solve this problem (circular dependent import) or the insert in order to avoid to instantiate the Project class?
Thanks