所以基本上我已经在几个小时内遇到这个flask_sqlachemy错误,尝试了多种解决方案在线,但是虽然我是这种语言和框架的新手,但是却无法正常工作。 sqlalchemy.exc.InvalidRequestError:表“处理”已定义 此MetaData实例。指定'extend_existing = True'重新定义 现有Table对象上的选项和列。
这是我的main.py
#main.py
from app import app
from db_setup import init_db, db_session
from forms import VisitSearchForm, TreatmentForm, PatientForm
from flask import flash, render_template, request, redirect
from models import Treatment, Visit, init_db, Patient
from tables import Results
from app import db
init_db()
@app.route('/', methods=['GET', 'POST'])
def index():
search = VisitSearchForm(request.form)
if request.method == 'POST':
return search_results(search)
return render_template('index.html', form=search)
@app.route('/results')
def search_results(search):
results = []
search_string = search.data['search']
if search.data['search'] == '':
qry = db_session.query(Treatment, Patient)
results = qry.all()
if not results:
flash('No results found!')
return redirect('/')
else:
# display results
table = Results(results)
table.border = True
return render_template('results.html', table=table)
@app.route('/new_treatment', methods=['GET', 'POST'])
def new_treatment():
"""
Add a new treatment
"""
form = TreatmentForm(request.form)
if request.method == 'POST' and form.validate():
# save the treatment
treatment = Treatment()
save_changes(treatment, form, new=True)
flash('Treatment Added successfully!')
return redirect('/')
return render_template('new_treatment.html', form=form)
@app.route('/new_patient', methods=['GET', 'POST'])
def new_patient():
"""
Add a new treatment
"""
form = PatientForm(request.form)
if request.method == 'POST' and form.validate():
# save the treatment
patient = Patient()
save_changes(patient, form, new=True)
flash('Patient Added successfully!')
return redirect('/')
return render_template('new_patient.html', form=form)
def save_changes(treatment, patient, form, new=False):
"""
Save the changes to the database
"""
# Get data from form and assign it to the correct attributes
# of the SQLAlchemy table object
visit = Visit()
visit.name = form.visit.data
patient.name = form.name.data
patient.address = form.address.data
patient.complaint = form.complaint.data
patient.email = form.email.data
treatment.visit = visit
treatment.name = form.name.data
treatment.treatment_date = form.treatment_date.data
treatment.notes = form.notes.data
treatment.price = form.price.data
treatment.treatment_type = form.treatment_type.data
db.create_all()
if new:
# Add the new treatment to the database
db_session.add(treatment)
# commit the data to the database
db_session.commit()
@app.route('/item/<int:id>', methods=['GET', 'POST'])
def edit(id):
qry = db_session.query(Treatment).filter(
Treatment.id==id)
treatment = qry.first()
if treatment:
form = TreatmentForm(formdata=request.form, obj=treatment)
if request.method == 'POST' and form.validate():
# save edits
save_changes(treatment, form)
flash('Visit updated successfully!')
return redirect('/')
return render_template('edit_visit.html', form=form)
else:
return 'Error loading #{id}'.format(id=id)
@app.route("/delete/<int:id>")
def delete(id):
Treatment.query.filter(Treatment.id == id).delete()
db.session.commit()
flash('Treatment Deleted successfully')
return redirect("/")
if __name__ == '__main__':
import os
if 'WINGDB_ACTIVE' in os.environ:
app.debug = False
app.run(port=5001)
这是我的模型。py
from app import db
class Visit(db.Model):
__tablename__ = "visits"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
def __repr__(self):
return "{}".format(self.name)
class Treatment(db.Model):
""""""
__tablename__ = "treatments"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
treatment_date = db.Column(db.String)
notes = db.Column(db.String)
treatment_type = db.Column(db.String)
price = db.Column(db.String)
visit_id = db.Column(db.Integer, db.ForeignKey("visits.id"))
visit = db.relationship("Visit", backref=db.backref(
"treatments", order_by=id), lazy=True)
class Patient(db.Model):
""""""
__tablename__ = "treatments"
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String)
address = db.Column(db.String)
complaint = db.Column(db.String)
email = db.Column(db.String)
def init_db():
db.create_all()
if __name__ == '__main__':
init_db()
db_setup.py
from sqlalchemy import create_engine
from sqlalchemy.orm import scoped_session, sessionmaker
from sqlalchemy.ext.declarative import declarative_base
engine = create_engine('sqlite:///patient.db', convert_unicode=True)
db_session = scoped_session(sessionmaker(autocommit=False,
autoflush=False,
bind=engine))
Base = declarative_base()
Base.query = db_session.query_property()
def init_db():
import models
Base.metadata.create_all(bind=engine)
db_creator.py
from sqlalchemy import create_engine, ForeignKey
from sqlalchemy import Column, Date, Integer, String
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, backref
engine = create_engine('sqlite:///patient.db', echo=True)
Base = declarative_base()
class Visit(Base):
__tablename__ = "visits"
id = Column(Integer, primary_key=True)
name = Column(String)
def __repr__(self):
return "<Visit: {}>".format(self.name)
class Treatment(Base):
""""""
__tablename__ = "treatments"
id = Column(Integer, primary_key=True)
name = Column(String)
treatment_date = Column(String)
notes = Column(String)
price = Column(String)
treatment_type = Column(String)
visit_id = Column(Integer, ForeignKey("visits.id"))
visit = relationship("Visit", backref=backref(
"treatments", order_by=id))
class Patient(Base):
""""""
__tablename__ = "patients"
id = Column(Integer, primary_key=True)
name = Column(String)
address = Column(String)
complaint = Column(String)
email = Column(String)
# create tables
Base.metadata.create_all(engine)
app.py
# app.py
from models import Treatment, Visit, init_db, Patient
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///patient.db'
app.secret_key = "flask method"
db = SQLAlchemy(app)
答案 0 :(得分:1)
似乎在您的models.py
文件中,您同时为__tablename__
和Treatment
模型提供了相同的Patient
属性。因此,存在冲突,因为两个表都试图将它们与相同的MetaData
只需将__tablename__
模型的Patient
更改为patient
。