gpt4 book ai didi

python - Pyramid :sqlalchemy.exc.OperationalError

转载 作者:搜寻专家 更新时间:2023-10-30 23:36:20 25 4
gpt4 key购买 nike

我正在学习 Pyramid 教程:http://pyramid-blogr.readthedocs.io/en/latest/project_structure.html并使用 Windows 8。

当前结果:当我打开本地主机地址(在 firefox 中)时,出现以下错误:

sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such    table: entries [SQL: 'SELECT entries.id AS entries_id, entries.title    AS entries_title, entries.body AS entries_body, entries.created AS    entries_created, entries.edited AS entries_edited \nFROM entries    ORDER BY entries.created DESC\n LIMIT ? OFFSET ?'] [parameters: (5,    0)]

追溯(最近的调用最后):

     File "C:\Python34\Lib\site-packages\pyramid_debugtoolbar\panels\performance.py",    line 71, in noresource_timer_handler
result = handler(request)
File "C:\Python34\Lib\site-packages\pyramid\tweens.py", line 62, in excview_tween
reraise(*attrs['exc_info'])
File "C:\Python34\Lib\site-packages\pyramid\compat.py", line 148, in reraise
raise value
File "C:\Python34\Lib\site-packages\pyramid\tweens.py", line 22, in excview_tween
response = handler(request)
File "C:\Python34\Lib\site-packages\pyramid_tm\__init__.py", line 119, in tm_tween
reraise(*exc_info)
File "C:\Python34\Lib\site-packages\pyramid_tm\compat.py", line 15, in reraise
raise value
File "C:\Python34\Lib\site-packages\pyramid_tm\__init__.py", line 98, in tm_tween
response = handler(request)
File "C:\Python34\Lib\site-packages\pyramid\router.py", line 158, in handle_request
view_name
File "C:\Python34\Lib\site-packages\pyramid\view.py", line 547, in _call_view
response = view_callable(context, request)
File "C:\Python34\Lib\site-packages\pyramid\viewderivers.py", line 442, in rendered_view
result = view(context, request)
File "C:\Python34\Lib\site-packages\pyramid\viewderivers.py", line 147, in _requestonly_view
response = view(request)
File "c:\projects\turing_chat\turing_blog\turing_blog\views\default.py", line 9, in index_page
paginator = BlogRecordService.get_paginator(request, page)
File "c:\projects\turing_chat\turing_blog\turing_blog\services\blog_record.py", line 30, in get_paginator
url_maker=url_maker)
File "C:\Python34\Lib\site-packages\paginate_sqlalchemy\__init__.py", line 35, in __init__
super(SqlalchemyOrmPage, self).__init__(*args, wrapper_class=SqlalchemyOrmWrapper, **kwargs)
File "C:\Python34\Lib\site-packages\paginate\__init__.py", line 251, in __init__
self.items = list(self.collection[first:last])
File "C:\Python34\Lib\site-packages\paginate_sqlalchemy\__init__.py", line 20, in __getitem__
return self.obj[range]
File "C:\Python34\Lib\site-packages\sqlalchemy\orm\query.py", line 2489, in __getitem__
return list(res)
File "C:\Python34\Lib\site-packages\sqlalchemy\orm\query.py", line 2797, in __iter__
return self._execute_and_instances(context)
File "C:\Python34\Lib\site-packages\sqlalchemy\orm\query.py", line 2820, in _execute_and_instances
result = conn.execute(querycontext.statement, self._params)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 945, in execute
return meth(self, multiparams, params)
File "C:\Python34\Lib\site-packages\sqlalchemy\sql\elements.py", line 263, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1053, in _execute_clauseelement
compiled_sql, distilled_params
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1189, in _execute_context
context)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1393, in _handle_dbapi_exception
exc_info
File "C:\Python34\Lib\site-packages\sqlalchemy\util\compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "C:\Python34\Lib\site-packages\sqlalchemy\util\compat.py", line 185, in reraise
raise value.with_traceback(tb)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\base.py", line 1182, in _execute_context
context)
File "C:\Python34\Lib\site-packages\sqlalchemy\engine\default.py", line 469, in do_execute
cursor.execute(statement, parameters) sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) no such table: entries [SQL: 'SELECT entries.id AS entries_id, entries.title AS entries_title, entries.body AS entries_body, entries.created AS entries_created, entries.edited AS entries_edited \nFROM entries ORDER BY entries.created DESC\n LIMIT ? OFFSET ?'] [parameters: (5, 0)]

预期结果:本教程指出,当我打开我的本地主机地址时,我应该看到一个包含文本的页面:“未找到博客条目”。


这些是我在教程中创建/编辑的一些文件的文件内容:

blog_record.py

> import sqlalchemy as sa from paginate_sqlalchemy import
> SqlalchemyOrmPage #<- provides pagination from ..models.blog_record
> import BlogRecord
>
>
> class BlogRecordService(object):
>
> @classmethod
> def all(cls, request):
> query = request.dbsession.query(BlogRecord)
> return query.order_by(sa.desc(BlogRecord.created)) # ^^ will return a query object that can return an entire dataset(in desc order)
> when needed.
> @classmethod
> def by_id(cls, _id, request):
> query = request.dbsession.query(BlogRecord)
> return query.get(_id) # ^^ return a single entry by id or the None object if nothing is found
> @classmethod
> def get_paginator(cls, request, page=1):
> query = request.dbsession.query(BlogRecord)
> query = query.order_by(sa.desc(BlogRecord.created))
> query_params = request.GET.mixed()
>
> def url_maker(link_page):
> # replace page param with values generated by paginator
> query_params['page'] = link_page
> return request.current_route_url(_query=query_params)
>
> return SqlalchemyOrmPage(query, page, items_per_page=5,
> url_maker=url_maker)

默认.py

from pyramid.view import view_config
from ..services.blog_record import BlogRecordService


@view_config(route_name='home',
renderer='pyramid_blogr:templates/index.jinja2')
def index_page(request):
page = int(request.params.get('page', 1))
paginator = BlogRecordService.get_paginator(request, page)
return {'paginator': paginator}
# ^^ We first retrieve from the URL's request object the page number that we want to present to the user.
# If the page number is not present, it defaults to 1.
# The paginator object returned by BlogRecord.get_paginator will then be used in the template
# to build a nice list of entries.
# Everything we return from our views in dictionaries will be available in templates as variables.
# So if we return {'foo':1, 'bar':2}, then we will be able to access the variables
# inside the template directly as foo and bar.

@view_config(route_name='auth', match_param='action=in', renderer='string',
request_method='POST')
@view_config(route_name='auth', match_param='action=out', renderer='string')
def sign_in_out(request):
return {}

用户.py

import datetime #<- will be used to set default dates on models
from turing_blog.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
from sqlalchemy import (
Column,
Integer,
Unicode, #<- will provide Unicode field
UnicodeText, #<- will provide Unicode text field
DateTime, #<- time abstraction field
)

class User(Base):
__tablename__ = 'users'
id = Column(Integer, primary_key=True)
name = Column(Unicode(255), unique=True, nullable=False)
password = Column(Unicode(255), nullable=False)
last_logged = Column(DateTime, default=datetime.datetime.utcnow)

模型/__init__.py

from sqlalchemy import engine_from_config
from sqlalchemy.orm import sessionmaker
from sqlalchemy.orm import configure_mappers
import zope.sqlalchemy

# import or define all models here to ensure they are attached to the
# Base.metadata prior to any initialization routines
from .user import User
from .blog_record import BlogRecord

# run configure_mappers after defining all of the models to ensure
# all relationships can be setup
configure_mappers()


def get_engine(settings, prefix='sqlalchemy.'):
return engine_from_config(settings, prefix)


def get_session_factory(engine):
factory = sessionmaker()
factory.configure(bind=engine)
return factory


def get_tm_session(session_factory, transaction_manager):
"""
Get a ``sqlalchemy.orm.Session`` instance backed by a transaction.

This function will hook the session to the transaction manager which
will take care of committing any changes.

- When using pyramid_tm it will automatically be committed or aborted
depending on whether an exception is raised.

- When using scripts you should wrap the session in a manager yourself.
For example::

import transaction

engine = get_engine(settings)
session_factory = get_session_factory(engine)
with transaction.manager:
dbsession = get_tm_session(session_factory, transaction.manager)

"""
dbsession = session_factory()
zope.sqlalchemy.register(
dbsession, transaction_manager=transaction_manager)
return dbsession


def includeme(config):
"""
Initialize the model for a Pyramid app.

Activate this setup using ``config.include('turing_blog.models')``.

"""
settings = config.get_settings()

# use pyramid_tm to hook the transaction lifecycle to the request
config.include('pyramid_tm')

session_factory = get_session_factory(get_engine(settings))
config.registry['dbsession_factory'] = session_factory

# make request.dbsession available for use in Pyramid
config.add_request_method(
# r.tm is the transaction manager used by pyramid_tm
lambda r: get_tm_session(session_factory, r.tm),
'dbsession',
reify=True
)

models/blog_record.py

import datetime #<- will be used to set default dates on models
from turing_blog.models.meta import Base #<- we need to import our sqlalchemy metadata from which model classes will inherit
from sqlalchemy import (
Column,
Integer,
Unicode, #<- will provide Unicode field
UnicodeText, #<- will provide Unicode text field
DateTime, #<- time abstraction field
)
from webhelpers2.text import urlify #<- will generate slugs e.g. converts "foo Bar Baz" to "foo-Bar-Baz"
from webhelpers2.date import distance_of_time_in_words #<- human friendly dates

class BlogRecord(Base):
__tablename__ = 'entries'
id = Column(Integer, primary_key=True)
title = Column(Unicode(255), unique=True, nullable=False)
body = Column(UnicodeText, default=u'')
created = Column(DateTime, default=datetime.datetime.utcnow)
edited = Column(DateTime, default=datetime.datetime.utcnow)

@property
def slug(self):
return urlify(self.title)

@property
def created_in_words(self):
return distance_of_time_in_words(self.created,
datetime.datetime.utcnow())

我怀疑我犯了一个简单的错误导致了这个错误,例如没有正确安装 Pyramid。如果有人可以帮助我,那就太好了。我也可以提供更多详细信息。

最佳答案

您需要运行创建数据库表的 initialize_pyramid_blogr_db 脚本。

你应该在某个地方有 virtualenv 文件夹,并且有 binScripts 文件夹,Python 包安装它们的命令行脚本。找到 initialize_pyramid_blogr_db 和它的完整文件系统路径。

1) 确保您已激活虚拟环境 (activate.bat)

2) 然后在你的项目文件夹中运行::

  python \path\bin\initialize_pyramid_blogr_db developmnt.ini

关于python - Pyramid :sqlalchemy.exc.OperationalError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41905148/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com