我正在使用Scrapy從網絡論壇中抓取數據。我使用SQLAlchemy將這些數據存儲在PostgreSQL數據庫中。表和列創建很好,但是,我無法讓SQLAlchemy在其中一列上創建索引。我正在嘗試使用杜松子酒創建trigram索引(pg_trgm)。如何使用SQLAlchemy for Scrapy創建pg_trgm索引?
PostgreSQL的代碼,將創建這個索引:
CREATE INDEX description_idx ON table USING gin (description gin_trgm_ops);
我已經加入到我的models.py文件中的SQLAlchemy的代碼是:
desc_idx = Index('description_idx', text("description gin_trgm_ops"), postgresql_using='gin')
我已經加入這一行我的模型.py但是當我檢查postgresql時,索引從未創建。
下面是我的完整models.py和pipelines.py文件。我有關這一切都錯了嗎?
任何幫助將不勝感激!
models.py:
from sqlalchemy import create_engine, Column, Integer, String, DateTime, Index, text
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_forum_table(engine):
DeclarativeBase.metadata.create_all(engine)
class forumDB(DeclarativeBase):
__tablename__ = "table"
id = Column(Integer, primary_key=True)
title = Column('title', String)
desc = Column('description', String, nullable=True)
desc_idx = Index('description_idx', text("description gin_trgm_ops"), postgresql_using='gin')
pipelines.py
from scrapy.exceptions import DropItem
from sqlalchemy.orm import sessionmaker
from models import forumDB, db_connect, create_forum_table
class ScrapeforumToDB(object):
def __init__(self):
engine = db_connect()
create_forum_table(engine)
self.Session = sessionmaker(bind=engine)
def process_item(self, item, spider):
session = self.Session()
forumitem = forumDB(**item)
try:
session.add(forumitem)
session.commit()
except:
session.rollback()
raise
finally:
session.close()
return item
謝謝!這工作完美! –