5

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

jj57347
  • 322
  • 2
  • 10
  • maybe it help: http://stackoverflow.com/questions/24872541/could-not-assemble-any-primary-key-columns-for-mapped-table – Evgenii Mar 13 '15 at 13:17
  • This question doesn't specify how the table is mapped, or what line raises that error, and adding a simple `mapper(Sale, sales_cache)` doesn't show any issues unless `primary_key=True` is removed from the table definition – dequis Aug 14 '16 at 05:20

0 Answers0