我对烧瓶有点新意,因此我无法执行代码,我实际上想要添加项目,同时添加添加时间以确定网页上添加的最新项目。但是当我尝试它时会抛出错误
OperationalError:(sqlite3.OperationalError)没有这样的列:items.date。
这是我的database.py
from sqlalchemy import Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship
from sqlalchemy import create_engine
Base = declarative_base()
class Item(Base):
__tablename__ = 'items'
title = Column(String(80), nullable=False)
id = Column(Integer, primary_key=True)
date = Column(DateTime, nullable=False)
description = Column(String(250))
category_id = Column(Integer, ForeignKey('category.id'))
category = relationship(Category)
engine = create_engine('sqlite:///catalogs.db')
Base.metadata.create_all(engine)
project_pc.py:
@app.route('/Category/<int:category_id>/items/new/', methods=['GET',
'POST'])
def newItem(category_id):
category = session.query(Category).filter_by(id=category_id).one()
if request.method == 'POST' :
newItem = Item(title=request.form['name'],
description = request.form['description'],
category_id = category_id,
date=datetime.now())
session.add(newItem)
session.commit()
flash('New %s Item Successfully Created' % (newItem.title))
return redirect(url_for('showItems', category_id=category_id))
else:
return render_template('newItem.html', category_id=category_id)
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line
1701, in __call__
return self.wsgi_app(environ, start_response)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1689, in wsgi_app
response = self.make_response(self.handle_exception(e))
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1687, in wsgi_app
response = self.full_dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1360, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1358, in full_dispatch_request
rv = self.dispatch_request()
File "/usr/local/lib/python2.7/dist-packages/flask/app.py", line 1344, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/vagrant/project_pc.py", line 234, in showItems
category_id=category_id).all()
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2588, in all
return list(self)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2736, in __iter__
return self._execute_and_instances(context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/query.py", line 2751, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1341, in _handle_dbapi_exception
exc_info
File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 200, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
context)
File "/usr/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
OperationalError: (sqlite3.OperationalError) no such column: items.date [SQL: u'SELECT items.title AS items_title, items.id AS items_id, items.date AS items_date, items.description AS items_description, items.category_id AS items_category_id, items.user_id AS items_user_id \nFROM items \nWHERE items.category_id = ?'] [parameters: (2,)
答案 0 :(得分:1)
我猜你首先在模型date
中没有Item
列的情况下创建了数据库。方法create_all
无法将此缺少的列添加到现有表中(它可以添加新表,但它不如migration tool强大。)
如果您可以删除数据库中的所有数据,只需删除文件catalogs.db
(或者更好地将其重命名为备份),然后create_all
方法将生成一个全新的工作数据库。 / p>
如果您已经拥有此数据库中的数据并且不想删除它们,则可以使用下面的内容从shell手动ALTER
表,但这种方式非常有限,因为{{1} } property需要应用于每个现有行的默认值,NOT NULL
命令不接受ALTER
或CURRENT_TIME
,只接受常量值。仅当您不关心使用今天或CURRENT_TIMESTAMP
这样的默认硬编码日期时,以下命令才有效:
"1900-01-01 00:00:00"
因此,您可能需要创建一个新表并复制所有行。看一下与此点完全匹配的问题:Add not null DateTime column to SQLite without default value?
答案 1 :(得分:0)
如果服务器仍在运行,请终止服务器。然后导航到包含catalogs.db的文件夹,并将其重命名为backups.db或其他名称。.您也可以将其删除。运行服务器,create all方法将自动为您创建catalogs.db。 我认为发生错误是因为您在创建后进行了一些更改。希望我也遇到了同样的问题,它将对您有所帮助。