金字塔:sqlalchemy.exc.OperationalError

时间:2017-01-28 00:30:36

标签: python html database sqlalchemy 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)

default.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 {}

user.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)

模型/ __初始化__。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
    )

模型/ 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())

我怀疑我犯了一个简单的错误,导致此错误,例如没有正确安装金字塔。如果有人可以帮助我会很棒。我也可以提供更多细节。

1 个答案:

答案 0 :(得分:1)

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

你应该在某处有virtualenv文件夹,并且有binScripts文件夹,其中Python包安装他们的命令行脚本。找到initialize_pyramid_blogr_db及其完整文件系统路径。

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

2)然后在项目文件夹::

中运行
  python \path\bin\initialize_pyramid_blogr_db developmnt.ini