我正在使用 Alembic 和 SQLAlchemy 开发一个项目,但在数据库中创建一个简单条目作为测试时遇到问题。我收到以下错误:
sqlalchemy.exc.ArgumentError: Mapper Mapper|Sale|sales_cache could not assemble any primary key columns for mapped table 'sales_cache'
我已经在下面的两个地方建立了主键(account_id),知道为什么 SQLAlchemy 无法识别它或如何修复它吗?我读过的其他答案都处理了多个/无主键的异常情况,并已相应解决,但这是一个非常普通的模型,不断失败。
我阅读了其他答案,其中大部分涉及声明性系统:
class Sale(Base):
__tablename__ = 'sales_cache'
但我需要使用经典的地图系统;这是我映射的类和架构,分别:
class Sale(object):
def __init__(self, notification):
self._sale_id = self._notification.object_id
self._account_id = self._notification.account_id
### schema.py file ###
from sqlalchemy.schema import MetaData, Table, Column
from sqlalchemy.types import (Unicode, Integer)
from database import metadata
metadata = MetaData()
sales_cache = Table('sales_cache', metadata,
Column('account_id', Integer, primary_key=True, autoincrement=False),
Column('sale_id', Integer, nullable=False)
)
这是我的 Alembic 修订版中的相关行:
sa.Column('account_id', sa.Integer(), primary_key=True, autoincrement=False),
我认为它可能会失败,因为我正在设置self._sale_id
and self._account_id
代替self.sale_id
and self.account_id
(没有下划线),但是当我也尝试这种方式时,没有任何改变。
提前致谢