我正在按照本教程关于在Postgres http://newcoder.io/scrape中使用Scrapy。我是postgres的新手,非常熟悉scrapy。
在教程中没有任何地方说明有关迁移的内容,因此我认为它打算使用DeclarativeBase.metadata.create_all(engine)
或代码内容在某处生成表。
我的models.py
from sqlalchemy import Table, create_engine, Column, Integer, String, DateTime
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL
import settings
DeclarativeBase = declarative_base()
def db_connect():
return create_engine(URL(**settings.DATABASE))
def create_scores_table(engine):
""""""
DeclarativeBase.metadata.create_all(engine)
class Scores(DeclarativeBase):
__tablename__ = "scores"
id = Column(Integer, primary_key=True)
score = Column('score', String, nullable=True)
我的pipelines.py
:
from sqlalchemy.orm import sessionmaker
from models import Scores, db_connect, create_scores_table
class RrsportsPipeline(object):
def __init__(self):
engine = db_connect()
create_scores_table(engine)
self.Session = sessionmaker(bind=engine)
def process_item(self, item, spider):
session = self.Session()
scores = Scores(**item)
try:
session.add(scores)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
return item
我知道数据库也正确连接了正确的信用卡和数据库名称。
完整的追踪
Traceback (most recent call last):
File "/usr/local/lib/python2.7/dist-packages/twisted/internet/defer.py", line 588, in _runCallbacks
current.result = callback(current.result, *args, **kw)
File "/home/ubuntu/rgsports/rgsports/rgsports/pipelines.py", line 44, in process_item
session.commit()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 801, in commit
self.transaction.commit()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 392, in commit
self._prepare_impl()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 372, in _prepare_impl
self.session.flush()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 2019, in flush
self._flush(objects)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 2137, in _flush
transaction.rollback(_capture_exception=True)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 60, in __exit__
compat.reraise(exc_type, exc_value, exc_tb)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/session.py", line 2101, in _flush
flush_context.execute()
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 373, in execute
rec.execute(self)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/unitofwork.py", line 532, in execute
uow
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 174, in save_obj
mapper, table, insert)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/orm/persistence.py", line 800, in _emit_insert_statements
execute(statement, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 914, in execute
return meth(self, multiparams, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/sql/elements.py", line 323, in _execute_on_connection
return connection._execute_clauseelement(self, multiparams, params)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1010, in _execute_clauseelement
compiled_sql, distilled_params
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1146, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1341, in _handle_dbapi_exception
exc_info
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/util/compat.py", line 202, in raise_from_cause
reraise(type(exception), exception, tb=exc_tb, cause=cause)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/base.py", line 1139, in _execute_context
context)
File "/usr/local/lib/python2.7/dist-packages/sqlalchemy/engine/default.py", line 450, in do_execute
cursor.execute(statement, parameters)
ProgrammingError: (psycopg2.ProgrammingError) column "score" of relation "scores" does not exist
LINE 1: INSERT INTO scores (score)
^
[SQL: 'INSERT INTO scores (score) VALUES (%(score)s) RETURNING scores.id'] [parameters: {'score': u'114'}]
但是当我跑蜘蛛时:
ProgrammingError: column "score" of relation "scores" does not exist
我应该手动创建表格吗?如果是这样,怎么样?我更喜欢在代码中使用它。我的模特中有什么东西我不见了吗?或者我是否必须将模型作为某种故意迁移运行?我似乎无法触发要构建的表。
答案 0 :(得分:1)
在pipeline.py中,您似乎需要导入create_scores_table而不是create_matchups_table
它的内容如下:
string msg = "word by word\n this is test";
string eachWord;
while( // something here ) {
// do some code here to get word by word
// but it should get white space and new line when it hits them
}
应为:
from sqlalchemy.orm import sessionmaker
from models import Scores, db_connect, create_matchups_table