python - SQLAlchemy Many-to-Many Relationship on a Single Table Error: NoReferencedTableError -
i'm using flask , sqlalchemy.
i'm trying reference column in same table create tree structure, using additional table store ancestor-child connections, not parent-child.
i've used example here https://stackoverflow.com/a/5652169/2947812, , code in models.py is:
# -*- coding: utf-8 -*- my_app import db cats_hierarchies = db.table('cats_hierarchies', db.metadata(), db.column('parent_id', db.integer, db.foreignkey('category.id')), db.column('category_id', db.integer, db.foreignkey('category.id')) ) class category(db.model): __tablename__ = 'category' id = db.column(db.integer, primary_key=true) children = db.relationship("category", secondary = cats_hierarchies, primaryjoin = cats_hierarchies.c.category_id == id, secondaryjoin = cats_hierarchies.c.parent_id == id, backref="children")
when try create database from my_app import db
receive following error message: sqlalchemy.exc.noreferencedtableerror: foreign key associated column 'cats_hierarchies.category_id' not find table 'category' generate foreign key target column 'id'
what doing wrong?
i got rid of error adding
foreign_keys = [cats_hierarchies.c.parent_id, cats_hierarchies.c.category_id])
to category.children definition.
still don't know though why necessary. found here: https://stackoverflow.com/a/8809907/2947812
Comments
Post a Comment