Flask SQLAlchemy Tables exist but still get no such table error
Question: I'm having a problem with my Flask database. I have opened the db file and
found that the tables exist but when I try to create a new user in the
database I get a no such table error.

here is my config.py file for the database

    
    
    CSRF_ENABLED = True
    SECRET_KEY = 'asshat'
    
    import os
    basedir = os.path.abspath(os.path.dirname(__file__))
    
    SQLALCHEMY_DATABASE_URI = 'sqlite:///' + os.path.join(basedir, 'app.db')
    SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
    

my app folder structure is

    
    
    pineapple/
    -------app.db
    -------config.py
    -------xapp.wsgi
    -------venv/
    -------app/
    -------------app.py
    -------------views.py
    -------------models.py
    -------------forms.py
    -------------static/
    -------------templates/
    

everything works fine when I open the site url but when I try to sign up with
it returns a 500 internal server error. so I check the apache error logs and I
get this

    
    
    [Fri Aug 08 02:09:55.819330 2014] [:error] [pid 12452] ERROR:app:Exception on /signup [ POST]
    [Fri Aug 08 02:09:55.819403 2014] [:error] [pid 12452] Traceback (most recent call last):
    [Fri Aug 08 02:09:55.819413 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
    [Fri Aug 08 02:09:55.819422 2014] [:error] [pid 12452]     response = self.full_dispatch_request()
    [Fri Aug 08 02:09:55.819430 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
    [Fri Aug 08 02:09:55.819439 2014] [:error] [pid 12452]     rv = self.handle_user_exception(e)
    [Fri Aug 08 02:09:55.819447 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
    [Fri Aug 08 02:09:55.819456 2014] [:error] [pid 12452]     reraise(exc_type, exc_value, tb)
    [Fri Aug 08 02:09:55.819464 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
    [Fri Aug 08 02:09:55.819473 2014] [:error] [pid 12452]     rv = self.dispatch_request()
    [Fri Aug 08 02:09:55.819481 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
    [Fri Aug 08 02:09:55.819490 2014] [:error] [pid 12452]     return self.view_functions[rule.endpoint](**req.view_args)
    [Fri Aug 08 02:09:55.819499 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/app/views.py", line 33, in signup
    [Fri Aug 08 02:09:55.819507 2014] [:error] [pid 12452]     db.session.commit()
    [Fri Aug 08 02:09:55.819515 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/scoping.py", line 150, in do
    [Fri Aug 08 02:09:55.819524 2014] [:error] [pid 12452]     return getattr(self.registry(), name)(*args, **kwargs)
    [Fri Aug 08 02:09:55.819532 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 776, in commit
    [Fri Aug 08 02:09:55.819540 2014] [:error] [pid 12452]     self.transaction.commit()
    [Fri Aug 08 02:09:55.819549 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 377, in commit
    [Fri Aug 08 02:09:55.819583 2014] [:error] [pid 12452]     self._prepare_impl()
    [Fri Aug 08 02:09:55.819591 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 357, in _prepare_impl
    [Fri Aug 08 02:09:55.819599 2014] [:error] [pid 12452]     self.session.flush()
    [Fri Aug 08 02:09:55.819606 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 1919, in flush
    [Fri Aug 08 02:09:55.819613 2014] [:error] [pid 12452]     self._flush(objects)
    [Fri Aug 08 02:09:55.819621 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2037, in _flush
    [Fri Aug 08 02:09:55.819629 2014] [:error] [pid 12452]     transaction.rollback(_capture_exception=True)
    [Fri Aug 08 02:09:55.819636 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 60, in     __exit__
    [Fri Aug 08 02:09:55.819644 2014] [:error] [pid 12452]     compat.reraise(exc_type, exc_value, exc_tb)
    [Fri Aug 08 02:09:55.819651 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/session.py", line 2001, in _flush
    [Fri Aug 08 02:09:55.819659 2014] [:error] [pid 12452]     flush_context.execute()
    [Fri Aug 08 02:09:55.819666 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 372, in execute
    [Fri Aug 08 02:09:55.819673 2014] [:error] [pid 12452]     rec.execute(self)
    [Fri Aug 08 02:09:55.819681 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/unitofwork.py", line 526, in execute
    [Fri Aug 08 02:09:55.819688 2014] [:error] [pid 12452]     uow
    [Fri Aug 08 02:09:55.819695 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 65, in save_obj
    [Fri Aug 08 02:09:55.819703 2014] [:error] [pid 12452]     mapper, table, insert)
    [Fri Aug 08 02:09:55.819711 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/orm/persistence.py", line 602, in _emit_insert_statements
    [Fri Aug 08 02:09:55.819719 2014] [:error] [pid 12452]     execute(statement, params)
    [Fri Aug 08 02:09:55.819726 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 729, in execute
    [Fri Aug 08 02:09:55.819734 2014] [:error] [pid 12452]     return meth(self, multiparams, params)
    [Fri Aug 08 02:09:55.819742 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/sql/elements.py", line 321, in _execute_on_connection
    [Fri Aug 08 02:09:55.819749 2014] [:error] [pid 12452]     return connection._execute_clauseelement(self, multiparams, params)
    [Fri Aug 08 02:09:55.819757 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 826, in _execute_clauseelement
    [Fri Aug 08 02:09:55.819765 2014] [:error] [pid 12452]     compiled_sql, distilled_params
    [Fri Aug 08 02:09:55.819772 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 958, in _execute_context
    [Fri Aug 08 02:09:55.819780 2014] [:error] [pid 12452]     context)
    [Fri Aug 08 02:09:55.819787 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 1160, in _handle_dbapi_exception
    [Fri Aug 08 02:09:55.819795 2014] [:error] [pid 12452]     exc_info
    [Fri Aug 08 02:09:55.819802 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/util/compat.py", line 199, in raise_from_cause
    [Fri Aug 08 02:09:55.819819 2014] [:error] [pid 12452]     reraise(type(exception), exception, tb=exc_tb)
    [Fri Aug 08 02:09:55.819828 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/engine/base.py", line 951, in _execute_context
    [Fri Aug 08 02:09:55.819835 2014] [:error] [pid 12452]     context)
    [Fri Aug 08 02:09:55.819842 2014] [:error] [pid 12452]   File "/var/www/html/pineapple-express/venv/lib/python2.7/site-packages/sqlalchemy/engine/default.py", line 436, in do_execute
    [Fri Aug 08 02:09:55.819850 2014] [:error] [pid 12452]     cursor.execute(statement, parameters)
    [Fri Aug 08 02:09:55.819858 2014] [:error] [pid 12452] OperationalError: (OperationalError) no such table: user u'INSERT INTO user (nickname, password, email, role) VALUES (?, ?, ?, ?)'
    

It tells me the tables don't exist but from physically looking at the database
using a database browser I know they are there. Can anyone tell me what I am
doing wrong? If there is any info I have failed to provide just ask and I'll
add it to the post.

**EDIT**

signup route from views

    
    
    @app.route('/signup', methods=['GET', 'POST'])
    def signup():
        form = Signup(request.form)
    
        if form.validate_on_submit():
            user = User(form.usrnam.data,form.passw.data,form.email.data)
            db.session.add(user)
            db.session.commit()
            os.makedirs('app/static/%s' % form.usrnam.data)
            flash('You are now a registered user')
            return redirect(url_for('login'))
    
        return render_template('signup.html', title='Sign Up', form=form)
    

**EDIT**

heres my User Model

    
    
    class User(db.Model):
        id = db.Column(db.Integer, primary_key = True)
        nickname = db.Column(db.String(64), index = True, unique = True)
        password = db.Column(db.String(10), index = True, unique = True)
        email = db.Column(db.String(120), index = True, unique = True)
        role = db.Column(db.SmallInteger, default = ROLE_USER)
        uploads = db.relationship('Upload', backref = 'author', lazy = 'dynamic')
        #registered_on = db.Column('registered_on' , db.DateTime)
    
        def __init__(self , nickname ,password , email):
            self.nickname = nickname
            self.password = password
            self.email = email
            #self.registered_on = datetime.utcnow()
    
        def is_authenticated(self):
            return True
    
        def is_active(self):
            return True
    
        def is_anonymous(self):
            return False
    
        def get_id(self):
            return unicode(self.id)
    
        def __repr__(self):
            return '<User %r>' % (self.nickname)
    


Answer: could it be that you need to capitalize User when referring to your table in
the code?

