Could not assemble any primary key columns for mapped table SQLAlchemy

Question:

When im tyring to run my FastAPI application this error appears.

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

When i remove the file d_tabelle.py everything works.

I think it can be a duplicate of this, but i doesn’t found an anwser for me.

I think everything is fine. With Tables O and V everything is ok.
I see no difference between Table V or O and Table D.

These 4 files are all in same directory d_tabelle.py, v_tabelle.py, o_tabelle.py,t_tabelle.py

d_tabelle.py:


from sqlmodel import SQLModel, Field, Relationship
from typing import Optional, List, Union
from datetime import datetime

from models.t_tabelle import TTabelle


class DTabelleBase(SQLModel):
    __tablename__ = 'd_tabelle'
    __table_args__ = {'schema': 'CDR_CIRPACK'}
    d_id: Optional[int] = Field(default=None, primary_key=True)
    dfi: datetime
    t_id: Optional[int] = Field(default=None, foreign_key="cdr_cirpack.t_tabelle.t_id")
    signalpointcode: Optional[str]
    callident: Optional[str]


class DTabelleRead(DTabelleBase):
    dfi: datetime


class DTabelle(SQLModel, table=True):
    # rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_d_tabelle_t_id')
    rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_d_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==DTabelle.t_id'})

o_tabelle.py:


from sqlmodel import SQLModel, Relationship, Field, Relationship
from typing import Optional, Union, List
from datetime import datetime

from models.t_tabelle import TTabelle


class OTabelleBase(SQLModel):
    __tablename__ = 'o_tabelle'
    __table_args__ = {'schema': 'CDR_CIRPACK'}
    o_id: Optional[int] = Field(default=None, primary_key=True)
    dfi: datetime
    t_id: Optional[int] = Field(default=None, foreign_key="cdr_cirpack.t_tabelle.t_id")
    dur: Optional[str]
    bytesend: Optional[str]
    byterec: Optional[str]
    packsent: Optional[str]
    packrec: Optional[str]
    packloss: Optional[str]
    averjitter: Optional[str]
    avertransdelay: Optional[str]
    addinfo: Optional[str]
    ipport: Optional[str]


class OTabelleRead(OTabelleBase):
    packSent: str
    packRec: str
    packLoss: str
    averJitter: str
    addInfo: str
    ipPort: str


class OTabelle(OTabelleBase, table=True):
    # rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_o_tabelle_t_id')
    rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_o_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==OTabelle.t_id'})


v_tabelle.py



from sqlmodel import SQLModel, Field, Relationship
from typing import Optional, List, Union
from datetime import datetime

from models.t_tabelle import TTabelle


class VTabelleBase(SQLModel):
    __tablename__ = 'v_tabelle'
    __table_args__ = {'schema': 'CDR_CIRPACK'}
    v_id: Optional[int] = Field(default=None, primary_key=True)
    dfi: datetime
    t_id: Optional[int] = Field(default=None, foreign_key="cdr_cirpack.t_tabelle.t_id")
    codecpaytype: Optional[str]
    ptime: Optional[str]
    silsupp: Optional[str]
    echocancel: Optional[str]
    event: Optional[str]
    addinfo: Optional[str]


class VTabelleRead(VTabelleBase):
    codecPayType: str
    echoCancel: str
    event: str
    addInfo: str


class VTabelle(VTabelleBase, table=True):
    # rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_v_tabelle_t_id')

    # # relations:
    rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(
        back_populates='rel_v_tabelle_t_id',
        sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==VTabelle.t_id'}
    )

t_tabelle.py


from sqlmodel import SQLModel, Field, Relationship
from typing import Optional, List
from datetime import datetime


class TTabelleBase(SQLModel):
    __tablename__ = 't_tabelle'
    __table_args__ = {'schema': 'CDR_CIRPACK'}
    t_id: Optional[int] = Field(default=None, primary_key=True)
    dfi: datetime
    flag: Optional[str]
    account: Optional[str]
    direction: Optional[str]
    cstartdate: Optional[str]
    cstarttime: Optional[str]
    condur: Optional[str]
    conringdur: Optional[str]
    totcalldur: Optional[str]
    ipaddr: Optional[str]
    accesscode: Optional[str]
    accesstype: Optional[str]
    pres: Optional[str]
    numplan_partynum: Optional[str]
    category: Optional[str]
    fwdcallind: Optional[str]
    natcallingnum: Optional[str]
    callingnum: Optional[str]
    nataddcallpartyaddr: Optional[str]
    addcallpartyaddr: Optional[str]
    accesstypecallnum: Optional[str]
    numplancallparty: Optional[str]
    natcallednum: Optional[str]
    callednum: Optional[str]
    catrealcallnum: Optional[str]
    typerealcallnum: Optional[str]
    natrealcallnum: Optional[str]
    realcallnum: Optional[str]
    billing: Optional[str]
    servcode: Optional[str]
    relloc: Optional[str]
    cause: Optional[str]
    opid: Optional[str]
    inccid: Optional[str]
    outcid: Optional[str]
    intrunkgroup: Optional[str]
    outtrunkgroup: Optional[str]
    unit: Optional[str]
    ts: Optional[datetime]


class TTabelleRead(TTabelleBase):
    t_id: int
    account: str
    direction: str
    cStartDate: str
    cStartTime: str
    conDur: str
    totCallDur: str
    pres: str
    natCallingNum: str
    callingNum: str
    addCallPartyAddr: str
    realCallNum: str
    relLoc: str
    cause: str
    inTrunkGroup: str
    outTrunkGroup: str


class TTabelle(TTabelleBase, table=True):
    rel_v_tabelle_t_id: List['VTabelle'] = Relationship(back_populates='rel_t_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==VTabelle.t_id'})
    rel_o_tabelle_t_id: List['OTabelle'] = Relationship(back_populates='rel_t_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==OTabelle.t_id'})
    rel_d_tabelle_t_id: List['DTabelle'] = Relationship(back_populates='rel_t_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==DTabelle.t_id'})


Asked By: Emil

||

Answers:

DTabelle doesn’t inherit from DTabelleBase. So it doesn’t have a primary key, ie. like d_id: Optional[int] = Field(default=None, primary_key=True). Whereas OTabelle does have a base class that has a primary key. At least as I understand this setup, I have not used sqlmodel before.

So either add a base class to DTabelle that has a primary key or add the primary key column directory to DTabelle.

Answered By: Ian Wilson

change this:

class DTabelle(SQLModel, table=True):
    rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_d_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==DTabelle.t_id'})

to this:

class DTabelle(DTabelleBase, table=True):
    rel_t_tabelle_t_id: Union[TTabelle, None] = Relationship(back_populates='rel_d_tabelle_t_id', sa_relationship_kwargs={'primaryjoin': 'TTabelle.t_id==DTabelle.t_id'})
Answered By: Emil