Flask-SQLAlchemy: there are no foreign keys linking these tables Flask-SQLAlchemy: there are no foreign keys linking these tables flask flask

Flask-SQLAlchemy: there are no foreign keys linking these tables


Flask-SQLAlchemy autogenerates the table name domain_root, with an underscore. You refer to domainroot without the underscore.

In general it's easier to define the fk and relationship in the same model, and avoid using string references.

from flask import Flaskfrom flask_sqlalchemy import SQLAlchemyapp = Flask(__name__)db = SQLAlchemy(app)class DomainRoot(db.Model):    id = db.Column(db.Integer, primary_key=True)class DomainPath(db.Model):    id = db.Column(db.Integer, primary_key=True)    root_id = db.Column(db.ForeignKey(DomainRoot.id))    root = db.relationship(DomainRoot, backref='paths')db.create_all()db.session.add(DomainRoot(paths=[DomainPath(), DomainPath()]))db.session.commit()print(DomainRoot.query.get(1).paths)
[<__main__.DomainPath object at 0x7fc27f443f28>, <__main__.DomainPath object at 0x7fc27f443fd0>]