title: sqlalchemy.exc.InvalidRequestError: Table Already defined
body: <p>so basically i have been getting this flask_sqlachemy error for hours tried mutiple solutions online but didn't work although i am new to this language and framework.
      sqlalchemy.exc.InvalidRequestError: Table 'treatments' is already defined 
      for this MetaData instance.  Specify 'extend_existing=True' to redefine 
      options and columns on an existing Table object.</p>
  
  <p>this is my main.py<br>
      # main.py</p>
  
  <pre><code>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/&lt;int:id&gt;', 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/&lt;int:id&gt;")
  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)
  </code></pre>
  
  <p>This is my models.py </p>
  
  <pre><code>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()
  </code></pre>
  
  <p>db_setup.py</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>db_creator.py</p>
  
  <pre><code>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 "&lt;Visit: {}&gt;".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)
  </code></pre>
  
  <p>app.py</p>
  
  <pre><code># 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)
  </code></pre>