sqlalchemy.exc.ArgumentError:创建backref时出错

时间:2014-12-10 11:13:57

标签: python sqlalchemy web-scraping scrapy

我正在尝试抓取数据并存储到数据库中,但它显示错误 sqlalchemy.exc.ArgumentError:创建backref' publisher_id'时出错关系' PublisherLookup.reviews':该名称的属性存在于mapper' Mapper |评论|评论'

from sqlalchemy import create_engine, Column, Integer, String, ForeignKey
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.engine.url import URL
from sqlalchemy.ext.declarative import synonym_for
from sqlalchemy.orm import sessionmaker, relationship
import settings
DeclarativeBase = declarative_base()

def db_connect():

# Performs database connection using database settings from settings.py.
# Returns sqlalchemy engine instance

    return create_engine(URL(**settings.DATABASE))



# <--snip-->


def create_publisher_lookup_table(engine):

    DeclarativeBase.metadata.create_all(engine)

class PublisherLookup(DeclarativeBase):
    __tablename__ = 'publisher_lookup'

    id = Column(Integer, primary_key=True)
    name = Column('name', String, nullable=True)
    rating_scale = Column('rating_scale', Integer, nullable=True)
    reviews = relationship("Reviews", backref='publisherid')

def create_reviews_table(engine):
# """"""

    DeclarativeBase.metadata.create_all(engine)

class Reviews(DeclarativeBase):
# """Sqlalchemy reviews model"""
    __tablename__ = 'reviews'

    id = Column(Integer, primary_key=True)
    content = Column('content', String, nullable=True)
    publisher_id = Column('publisher_id', Integer, ForeignKey('publisher_lookup.id'))
    # rating = Column('rating_scale', Integer )
    # description = Column('description', String, nullable=True)
    # link = Column('link', String, nullable=True)
    # location = Column('location', String, nullable=True)
    # category = Column('category', String, nullable=True)
    # original_price = Column('original_price', String, nullable=True)
    # price = Column('price', String, nullable=True)
    publisherid = relationship(PublisherLookup, primaryjoin=publisher_id == PublisherLookup.id)

这是错误

Traceback (most recent call last):
  File "/usr/lib/python2.7/dist-packages/scrapy/middleware.py", line 60, in _process_chain
    return process_chain(self.methods[methodname], obj, *args)
  File "/usr/lib/python2.7/dist-packages/scrapy/utils/defer.py", line 65, in process_chain
    d.callback(input)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 382, in callback
    self._startRunCallbacks(result)
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 490, in _startRunCallbacks
    self._runCallbacks()
--- <exception caught here> ---
  File "/usr/lib/python2.7/dist-packages/twisted/internet/defer.py", line 577, in _runCallbacks
    current.result = callback(current.result, *args, **kw)
  File "/home/suz/laravel/massblurb/massblurb/template/template/pipelines.py", line 29, in process_item
    reviews = Reviews(**item)
  File "<string>", line 2, in __init__

  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py", line 317, in _new_state_if_none
    state = self._state_constructor(instance, self)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/util/langhelpers.py", line 612, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/instrumentation.py", line 152, in _state_constructor
    self.dispatch.first_init(self, self.class_)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/event.py", line 409, in __call__
    fn(*args, **kw)
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2260, in _event_on_first_init
    configure_mappers()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 2169, in configure_mappers
    mapper._post_configure_properties()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/mapper.py", line 1281, in _post_configure_properties
    prop.init()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/interfaces.py", line 231, in init
    self.do_init()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 1033, in do_init
    self._generate_backref()
  File "/usr/lib/python2.7/dist-packages/sqlalchemy/orm/properties.py", line 1222, in _generate_backref
    self, m))
sqlalchemy.exc.ArgumentError: Error creating backref 'content' on relationship 'PublisherLookup.reviews': property of that name exists on mapper 'Mapper|Reviews|reviews'

1 个答案:

答案 0 :(得分:0)

backref想要使用给定名称在引用的表上创建属性/属性。在您的情况下,已经有一个名为publisher_id的属性。 backref不应该是ForeignKey列的名称,而应该是关系另一侧的名称。在您的情况下,它可能应该命名为publisher_lookup

class PublisherLookup(DeclarativeBase):
    # ...
    reviews = relationship("Reviews", backref='publisher_lookup')