在使用db.relationship尝试“烧瓶db init”或“烧瓶运行”时,我不断收到以下错误:
TypeError: relationship() got an unexpected keyword argument 'query_class'
如果我注释掉db.relationship行,那么它将正常工作。
我正在尝试通过使用下面的部分文档代码来实现这一目标:
import os
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config['SECRET_KEY'] = 'mysecretkey'
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+os.path.join(basedir,'data.sqlite')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
db = SQLAlchemy(app)
Migrate(app,db)
class Person(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(50), nullable=False)
addresses = db.relationship('Address', backref='person', lazy=True)
class Address(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(120), nullable=False)
person_id = db.Column(db.Integer, db.ForeignKey('person.id'),
nullable=False)
环境:
-烧瓶1.1.2
-Flask-Migrate 2.5.3
-Flask-SQLAlchemy 2.4.3
-SQLAlchemy 1.3.17
-PyCharm 2020.1.1
-Windows 7
答案 0 :(得分:0)
我设法通过卸载Flask,Flask-Migrate和SQLAlchemy然后重新安装来解决此问题。
#Uninstall
pip uninstall Flask
pip uninstall Flask-Migrate
pip uninstall SQLAlchemy
#Install
pip install Flask
pip install Flask-Migrate
pip install SQLAlchemy