title: OperationalError no such table: use when adding new users to SQLite database [fixed]
body: <p>I'm making a website using Flask and using flasksqlalchemy for making new users, but when I commit to add the new user to the database, I get a sqlalchemy.exc.OperationalError.</p>
  <p>Here is the code for my models.py file:</p>
  <pre class="lang-py prettyprint-override"><code>from .__init__ import db
  from flask_login import UserMixin
  from sqlalchemy.sql import func
  
  class Reservation(db.Model):
      id = db.Column(db.Integer , primary_key=True)
      data = db.Column(db.String(128))
      date = db.Column(db.DateTime(timezone=True), default=func.now())
      user_id = db.Column(db.Integer, db.ForeignKey(&quot;user.id&quot;))
  
  class user(db.Model, UserMixin):
      id = db.Column(db.Integer, primary_key=True)
      userName = db.Column(db.String(128))
      password = db.Column(db.String(128), unique=True)
      reservations = db.relationship(&quot;Reservation&quot;)
  </code></pre>
  <p>Here's the code for my <strong>init</strong>.py file:</p>
  <pre class="lang-py prettyprint-override"><code>from flask import Flask
  from flask_sqlalchemy import SQLAlchemy
  from os import path
  
  db = SQLAlchemy()
  DB_NAME = &quot;database.db&quot;
  
  
  def create_app():
      app = Flask(__name__)
      app.config['SECRET_KEY'] = 'secret'
      app.config[&quot;SQLALCHEMY_DATABASE_URI&quot;] = f&quot;sqlite:///{DB_NAME}&quot;
      print(&quot;Initializing app&quot;)
      db.init_app(app)
      print(&quot;App initialized&quot;)
  
      print(&quot;Importing blueprints...&quot;)
      from .views import views
      from .auth import auth
      
      app.register_blueprint(views, url_prefix=&quot;/&quot;)
      app.register_blueprint(auth, url_prefix=&quot;/&quot;)
      print(&quot;Registered Blueprints&quot;)
      print(&quot;Importing database&quot;)
      from .models import user, Reservation
  
      print(&quot;Creating database&quot;)
      create_database(app)
      db.create_all(app=app)
      
  
      return(app)
  
  def create_database(app):
      print(&quot;Checking if DB exists&quot;)
      if not path.exists(&quot;website/&quot; + DB_NAME):
          db.create_all(app=app)
          print(&quot;DB did not exist, has been created right now.&quot;)
      else: 
          print(&quot;DB exists, nothing has been done.&quot;)
  </code></pre>
  <p>And here is the code giving me an error:</p>
  <pre class="lang-py prettyprint-override"><code>print(&quot;Adding user to database&quot;)
  new_user = user(userName=username, password=gph(password,method=&quot;sha256&quot;))
  db.session.add(new_user)
  db.session.commit()
  </code></pre>
  <p>Here is the error log:
  sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: user
  [SQL: INSERT INTO user (&quot;userName&quot;, password) VALUES (?, ?)]
  [parameters: ('bingusCoder', 'sha256$vYx5OSQyP182M8Jw$85e0690712b68b4b11e181904b44387eba2c1e1cdacebfbb4a60426c4ecdee81')]
  (Background on this error at: <a href="https://sqlalche.me/e/14/e3q8" rel="nofollow noreferrer">https://sqlalche.me/e/14/e3q8</a>)</p>
  <p><a href="https://i.stack.imgur.com/FpL6T.png" rel="nofollow noreferrer">More info on errors</a></p>
  <p>Edit: fixed it by putting all code into 1 file</p>