本文介绍了sqlalchemy.exc.ArgumentError:创建反向引用时出错的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试抓取数据并存储到数据库中,但显示错误sqlalchemy.exc.ArgumentError:在关系PublisherLookup.reviews"上创建反向引用publisher_id"时出错:映射器Mapper|Reviews|reviews"上存在该名称的属性

i am trying to scrape data and store into database but its showing an error sqlalchemy.exc.ArgumentError: Error creating backref 'publisher_id' on relationship 'PublisherLookup.reviews': property of that name exists on mapper 'Mapper|Reviews|reviews'

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'

推荐答案

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

backref wants to create an attribute/property on the referenced table with the given name. In your case there is already a property called publisher_id. backref should not be the name of the ForeignKey column, but rather the name of the other side of the relationship. In your case it probably should be named publisher_lookup:

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

这篇关于sqlalchemy.exc.ArgumentError:创建反向引用时出错的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 11:00