Reputation: 372
I have a flask application which it's packages are separated each other using Blueprints. I have 3 packages which have many to one relations.
The first package (blueprint) is Authentication (Containts User_Model):
from Billboard.Apps.models import Android_Model
from Billboard.Survey.models import Survey_Model
class User_Model (db.Model, UserMixin):
__tablename__ = user_model
...
advertised_apps = db.relationship ('Android_Model' , backref = 'user_model' , lazy = True)
advertised_surveys = db.relationship ('Survey_Model' , backref = 'user_model' , lazy = True)
the second package has foreign key to my User_Model:
from Billboard.Authentication.models import User_Model
class Survey_Model (db.Model):
__tablename__ = 'survey_model'
...
advertiser_id = db.Column(db.Integer, db.ForeignKey('user_model.id'), nullable=False)
And the third package's model is same as second one.
When i run my flask app i got some imports error which i know they are for circular import between these models.
NOTE: when i remove this relation between these models, i got no error and project works correctly.
Any idea to solve?
Upvotes: 2
Views: 246
Reputation: 173
You don't actually need to import the model to declare a relationship with it. Notice db.relationship
takes a string as a first argument and not a class.
Upvotes: 1