0

My model base class is like below

from sqlalchemy.ext.declarative import as_declarative
from sqlalchemy.ext.declarative import declared_attr


@as_declarative()
class Base:
    __name__: str

    # to generate tablename from classname
    @declared_attr
    def __tablename__(cls) -> str:
        return cls.__name__.lower()

And my model ( using sqlmodel pattern) looks like below

from typing import Optional    
from sqlmodel import  Field    
import Base

class Product(Base):

    id: Optional[int] = Field(None, primary_key=True)
    name: str

Now when I am starting my application for table creation its complaining

sqlalchemy.exc.ArgumentError: Mapper mapped class Product->product could not assemble any primary key columns for mapped table 'product'

Dibshare
  • 95
  • 2
  • 13
  • You could _try_ `@as_declarative(cls=SQLModel)`. Note that SQLModel [already lower-cases the table name](https://github.com/tiangolo/sqlmodel/blob/75ce45588b6a13136916929be4c44946f7e00cbd/sqlmodel/main.py#L644) – snakecharmerb Sep 07 '22 at 13:17

0 Answers0