Flask和SQLAlchemy db.session.commit无法正常工作 [英] Flask and SQLAlchemy db.session.commit not working
问题描述
当我将测试用户添加到 User
表时,它添加的很好,即使我执行 db.session.add(u)
也可以,但是当我执行 db.session.commit()
时抛出错误.
When I add a test user to the User
table, it adds fine and it is fine even when I execute db.session.add(u)
, but it throws error when I execute db.session.commit()
.
from app import db
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Integer(120), unique=True)
email = db.Column(db.Integer(120), unique=True)
configkey = db.Column(db.Integer(200), unique=True)
dob = db.Column(db.DateTime)
country = db.Column(db.Integer(120))
friends = db.relationship('Friend', backref = 'friendof', lazy = 'dynamic')
def __repr__(self):
return '<User %r>' % (self.name)
class Friend(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.Integer(120), unique=True)
email = db.Column(db.Integer(120), unique=True)
dob = db.Column(db.DateTime)
country = db.Column(db.Integer(120))
lastmessage = db.Column(db.Integer(1500))
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))
def __repr__(self):
return '<FriendName %r>' % (self.name)
-编辑,包括Python控制台中的错误消息:
--Edit, including the error message in Python console:
>>> u = models.User(name="sd", email="e")
>>> db.session.add(u)
>>> db.session.commit()
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/scoping.py", line 149, in do
return getattr(self.registry(), name)(*args, **kwargs)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 721, in commit
self.transaction.commit()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 354, in commit
self._prepare_impl()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 334, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1818, in flush
self._flush(objects)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1936, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 58, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 1900, in _flush
flush_context.execute()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 372, in execute
rec.execute(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 525, in execute
uow
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 64, in save_obj
table, insert)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 569, in _emit_insert_statements
execute(statement, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 662, in execute
params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 761, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 874, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1024, in _handle_dbapi_exception
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 196, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 867, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 324, in do_execute
cursor.execute(statement, parameters)
sqlalchemy.exc.OperationalError: (OperationalError) no such table: user u'INSERT INTO user (name, email, configkey, dob, country) VALUES (?, ?, ?, ?, ?)' ('sd'
, 'e', None, None, None)
推荐答案
我正在查看您的堆栈跟踪(特别是最后一行),看起来您的表尚不存在.
I'm looking at your stack trace (specifically the last line) and it looks as though your table doesn't exist yet.
在将新模型与数据库一起使用之前,必须存在表和列.好消息是SQLAlchemy可以为您做到这一点.
Before you can use your new models with your database the tables and columns must be present. The good news is that SQLAlchemy can do this for you.
尝试从程序中的任何位置调用以下内容:
Try calling the following from anywhere within your program:
# Creates the schema on the database
db.create_all()
完成操作后,请务必将其删除!
Just be sure to remove this when you're done!
理想情况下,您会从命令行中调用另一个文件,该文件会导入flask应用程序,然后运行此命令.例如...
Ideally you'd have another file you call from the command line that imports your flask app and then runs this command. For example...
# Replace these with your main file and the name of your flask object
from myapp import app
# And this one with the name of your database file and object
from database import db
db.create_all()
print "Database ready!"
修改: db.create_all()
命令不会修改现有表,类似地,在运行 db.create_all()
之后,表也不会自动更改自身.但是,您可以例如创建一个新表,然后重新运行该函数并使它显示.
The db.create_all()
command won't modify existing tables, similarly tables won't automagically change themselves after you've run db.create_all()
. You can however for example, create a new table and then re-run the function and have it appear.
不过,为了正确处理数据库升级,我建议调查 Flask迁移,尤其是如果您的应用最终将在生产环境中使用.
To properly handle upgrades to your database though, I recommend looking into Flask-Migrate, especially if your app will eventually be used in a production environment.
在最初的开发过程中,我使用的快速而肮脏的方法是删除数据库,如果更改太大,则重新创建它.
The quick and dirty method I use during initial development is to just delete the database and create it again if the changes are too large.
这篇关于Flask和SQLAlchemy db.session.commit无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!