Flask SQLAlchemy表存在,但仍然没有这样的表错误

时间:2014-08-08 06:40:42

标签: python database flask flask-sqlalchemy

我的Flask数据库出了问题。我打开了db文件,发现表存在,但是当我尝试在数据库中创建一个新用户时,我得到了一个没有这样的表错误。

这是我的数据库的config.py文件

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')

我的app文件夹结构是

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

当我打开网站网址时,一切正常,但当我尝试使用它注册时,会返回500内部服务器错误。所以我检查了apache错误日志,我得到了这个

[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 (?, ?, ?, ?)'

它告诉我表格不存在但是从使用数据库浏览器物理查看数据库我知道它们在那里。谁能告诉我我做错了什么?如果有任何信息,我没有提供,只是问我,我会把它添加到帖子。

修改

来自观看的注册路线

@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)

修改

继承我的用户模型

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)

1 个答案:

答案 0 :(得分:-1)

是否需要在代码中引用您的表时将用户大写?