SQLAlchemy classical mapper “could not assemble any primary key columns for mapped table” despite presence of a primary key?

╄→гoц情女王★ 提交于 2019-12-23 10:51:12

问题


I'm working on a project with Alembic and SQLAlchemy, but I'm having trouble creating a simple entry in the database as a test. I get the following error:

sqlalchemy.exc.ArgumentError: Mapper Mapper|Sale|sales_cache could not assemble any primary key columns for mapped table 'sales_cache'

I've established the primary key (account_id) in both places below, any idea why SQLAlchemy doesn't recognize that or how to fix it? The other answers I've read have all dealt with exception cases for multiple/no primary keys, and have been solved accordingly, but this is a pretty vanilla model that keeps failing.

I've read up on other answers, most of which deal with the declarative system:

class Sale(Base):
    __tablename__ = 'sales_cache'

But I'm required to use the classical mapping system; here's my mapped class and schema, respectively:

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)
)

And this is the relevant line from my alembic revision:

sa.Column('account_id', sa.Integer(), primary_key=True, autoincrement=False),

I thought it might be failing because I was setting self._sale_id and self._account_id instead of self.sale_id and self.account_id (without the underscore), but nothing changed when I tried it this way too.

Thanks in advance

来源:https://stackoverflow.com/questions/27933961/sqlalchemy-classical-mapper-could-not-assemble-any-primary-key-columns-for-mapp

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!