我正在尝试为现有数据库/架构/表添加多对多关系。我只有一个数据库和几个架构。这是我的代码
followers = db.Table('followers', db.Model.metadata,
db.Column('follower_id', db.Integer, db.ForeignKey('avengers_user.users.id')),
db.Column('followed_id', db.Integer, db.ForeignKey('avengers_user.users.id')),
)
class User(UserMixin, db.Model):
__tablename__ = 'users'
__bind_key__ = 'avengers'
__table_args__ = {"schema": "avengers_user"}
id = db.Column(db.Integer, autoincrement=True, primary_key=True, unique=True)
username = db.Column(db.TEXT, nullable=False)
followed = db.relationship(
'User', secondary=followers,
primaryjoin=(followers.c.follower_id == id),
secondaryjoin=(followers.c.followed_id == id),
backref=db.backref('avengers_user.followers', lazy='dynamic'), lazy='dynamic')
当我运行flask数据库迁移时。出现错误
sqlalchemy.exc.NoReferencedTableError: Foreign key associated
with column 'avengers_user.followers.followed_id' could not fi
nd table 'avengers_user.users' with which to generate a foreig
n key to target column 'id'
表用户存在(我有一千个用户...)
非常感谢您的帮助!
我应该在以下miquels教程中注意我。.https://blog.miguelgrinberg.com/post/the-flask-mega-tutorial-part-viii-followers