来自ORM的SQLAlchemy实例化对象失败,出现AttributeError:mapper

时间:2015-10-16 01:48:31

标签: python sqlalchemy

我一直试图在后端使用SQLAlchemy来获得一个像样的大小项目。我有多个文件的表模型,自己文件中的声明性基础,以及用于包装常见SQLAlchemy函数和驱动程序文件的帮助文件。

我上传了数据,然后决定添加一个列。由于这只是测试数据,我认为最简单的方法是删除所有表并开始新的...然后当我尝试重新创建模式和表时,公共声明性基类突然有空元数据。我通过导入类声明文件解决了这个问题 - 奇怪的是,因为我之前不需要那些导入 - 并且它能够成功地重新创建模式。

但是现在当我再次尝试创建对象时,我收到一个错误:

AttributeError: mapper

现在我完全糊涂了!有人能解释一下这里发生了什么吗?在我放弃架构之前它工作正常,现在我无法正常工作。

这是我设置的骨架:

base.py

from sqlalchemy.ext.declarative import declarative_base
Base = declarative_base()

models1.py

from base import Base
class Business(Base):
    __tablename__ = 'business'
    id = Column(Integer, primary_key=True)

models2.py:

from base import Base
class Category(Base):
    __tablename__ = 'category'
    id = Column(Integer, primary_key=True)

helper.py:

from base import Base

# I didn't need these two imports the first time I made the schema
# I added them after I was just getting an empty schema from base.Base
# but have no idea why they're needed now?
import models1
import models2

def setupDB():
    engine = getDBEngine(echo=True) # also a wrapped func (omitted for space)
    #instantiate the schema
    try:
        Base.metadata.create_all(engine, checkfirst=True)
        logger.info("Successfully instantiated Database with model schema")
    except:
        logger.error("Failed to instantieate Database with model schema")
        traceback.print_exc()

def dropAllTables():
    engine = getDBEngine(echo=True)
    # drop the schema
    try:
        Base.metadata.reflect(engine, extend_existing=True)
        Base.metadata.drop_all(engine)
        logger.info("Successfully dropped all the database tables in the schema")
    except:
        logger.error("Failed to drop all tables")
        traceback.print_exc()

driver.py:

import models1
import models2

# ^ some code to get to this point
categories []
categories.append(
                models2.Category(alias=category['alias'],
                                 title=category['title']) # error occurs here
                )

堆栈跟踪:(完整性)

File "./main.py", line 16, in <module>
yelp.updateDBFromYelpFeed(fname)
  File "/Users/thomaseffland/Development/projects/health/pyhealth/pyhealth/data/sources/yelp.py", line 188, in updateDBFromYelpFeed
    title=category['title'])
  File "<string>", line 2, in __init__
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 347, in _new_state_if_none
    state = self._state_constructor(instance, self)
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 747, in __get__
    obj.__dict__[self.__name__] = result = self.fget(obj)
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/instrumentation.py", line 177, in _state_constructor
    self.dispatch.first_init(self, self.class_)
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/event/attr.py", line 256, in __call__
    fn(*args, **kw)
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2825, in _event_on_first_init
    configure_mappers()
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 2721, in configure_mappers
    mapper._post_configure_properties()
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/mapper.py", line 1710, in _post_configure_properties
    prop.init()
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/interfaces.py", line 183, in init
    self.do_init()
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1616, in do_init
    self._process_dependent_arguments()
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/orm/relationships.py", line 1673, in     _process_dependent_arguments
    self.target = self.mapper.mapped_table
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 833, in __getattr__
    return self._fallback_getattr(key)
  File "/Users/thomaseffland/.virtualenvs/health/lib/python2.7/site-packages/sqlalchemy/util/langhelpers.py", line 811, in _fallback_getattr
    raise AttributeError(key)
AttributeError: mapper

我知道这篇文章很长,但我想提供完整的图片。首先,我很困惑为什么base.Base架构首先是空的。现在我很困惑为什么Categories对象缺少mapper!

非常感谢任何帮助/见解/建议,谢谢!

修改

因此模型文件和helper.py在supackage中,driver.py实际上是兄弟子包中的文件,其代码包含在一个函数中。此驱动程序函数由包级主文件调用。所以我不认为这可能是因为SQLAlchemy还没有时间初始化? (如果我理解答案正确的话)这里是主文件的(相关部分)的样子:

main.py:

import models.helper as helper
helper.setupDB(echo=true) # SQLAlchemy echos the correct statements

import driverpackage.driver as driver
driver.updateDBFromFile(fname) # error occurs in here

driver.py 实际上如下:

import ..models.models1
import ..models.models2

def updateDBFromFile(fname):
    # ^ some code to get to this point
    categories []
    categories.append(
                    models2.Category(alias=category['alias'],
                                     title=category['title']) # error occurs here
                    )
    # a bunch more code

编辑2: 我开始怀疑潜在的问题与我突然需要导入所有模型以在helper.py中设置架构的原因相同。如果我打印导入的模型对象的表,它们没有绑定的MetaData或模式:

print YelpCategory.__dict__['__table__'].__dict__
####
{'schema': None, '_columns': <sqlalchemy.sql.base.ColumnCollection object at 0x102312ef0>, 
'name': 'yelp_category', 'description': 'yelp_category', 
'dispatch': <sqlalchemy.event.base.DDLEventsDispatch object at 0x10230caf0>, 
'indexes': set([]), 'foreign_keys': set([]), 
'columns': <sqlalchemy.sql.base.ImmutableColumnCollection object at 0x10230fc58>, 
'_prefixes': [], 
'_extra_dependencies': set([]), 
'fullname': 'yelp_category', 'metadata': MetaData(bind=None), 
'implicit_returning': True, 
'constraints': set([PrimaryKeyConstraint(Column('id', Integer(), table=<yelp_category>, primary_key=True, nullable=False))]), 'primary_key': PrimaryKeyConstraint(Column('id', Integer(), table=<yelp_category>, primary_key=True, nullable=False))}

我想知道为什么来自创建数据库的基础的元数据没有被绑定?

2 个答案:

答案 0 :(得分:0)

我猜错误的发生是因为您在Python模块级执行代码。当Python导入模块时执行此代码。

  • 将代码移至函数。

  • 在SQLAlchemy正确初始化后调用该函数。

  • DBSession.configure(bind=engine)只需在安装应用程序时调用一次,因为在数据库中持久创建的表

  • 您需要\xFF\xC2 \xFF\xDA 或相关信息,它会告诉模型它们与哪些数据库连接相关。问题中缺少这个。

答案 1 :(得分:0)

问题似乎已经消失,所以我会发布我的工作。这很简单。我重构了代码以明确地提供类和经典的映射器,而不是使用声明性基础,一切都运行良好......