Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Flask and SQLalchemy NoForeignKeysError: Could not determine join condition between parent/child tables on relationship

Ask Question
@dataclass
class Plans(db.Model):
    id = db. Column(db.Integer, primary_key=True, autoincrement=False)
    stripe_plan_id = db.Column(db.String(100), nullable=True,)
    name = db.Column(db.String(100), nullable=True,)
    is_deleted = db.Column(db.Boolean, default=False)
    # feature = db.relationship('PlanFeatures', backref = 'planfeatures')
    def __str__(self):
        return str(self.name)
    @property
    def serialize(self):
        """Return object data in easily serializable format"""
        return {
            'id': self.id,
            'name': self.name,
            'stripe_plan_id': self.stripe_plan_id,
            'is_deleted': self.is_deleted,
@dataclass
class PlanFeatures(db.Model):
    id = db. Column(db.Integer, primary_key=True, autoincrement=False)
    plan = db.relationship(
        'Plans', back_populates='planfeatures')
    feature = db.Column(db.String(100), nullable=True,)
    is_deleted = db.Column(db.Boolean, default=False)
    def __str__(self):
        return str(self.feature)
    @property
    def serialize(self):
        """Return object data in easily serializable format"""
        return {
            'id': self.id,
            'plan': self.plan,
            'feature': self.feature,
            'is_deleted': self.is_deleted,

I am working on db sync with 2 programs one being django and other being flask. But when I try it, this error

sqlalchemy.exc.NoForeignKeysError: Could not determine join condition between parent/child tables on relationship PlanFeatures.plan - there are no foreign keys linking these tables.  Ensure that referencing columns are associated with a ForeignKey or ForeignKeyConstraint, or specify a 'primaryjoin' expression.

I strongly believe this is something with the model design. Thanks in advance

You need to have ForeignKey field in your PlanFeatures to correctly set relationship()

class PlanFeatures(db.Model):
    id = db. Column(db.Integer, primary_key=True, autoincrement=False)
    plan_id = db.Column(db.Integer, db.ForeignKey('plans.id'))
    feature = db.Column(db.String(100), nullable=True,)
    is_deleted = db.Column(db.Boolean, default=False)
    plan = db.relationship('Plans', back_populates='planfeatures')

relationship() does only create mapping between classes and doesn't create any columns

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.