python config_error ai_generated true

sqlalchemy.exc.InvalidRequestError: Could not assemble any primary key columns for mapped table 'orders'

ID: python/sqlalchemy-could-not-assemble-mapper

Also available as: JSON · Markdown · 中文
80%Fix Rate
90%Confidence
0Evidence
2024-05-18First Seen

Version Compatibility

VersionStatusIntroducedDeprecatedNotes
3.x active

Root Cause

The mapped table lacks a primary key definition, either because no column is marked as primary_key or because the table inherits from a select that doesn't have one.

generic

中文

映射表缺少主键定义,可能因为没有列被标记为主键,或表继承自没有主键的 select。

Workarounds

  1. 100% success
    class Order(Base):
        __tablename__ = 'orders'
        id = Column(Integer, primary_key=True)
        # other columns
  2. 95% success
    class OrderItem(Base):
        __tablename__ = 'order_items'
        order_id = Column(Integer, ForeignKey('orders.id'), primary_key=True)
        product_id = Column(Integer, primary_key=True)

Dead Ends

Common approaches that don't work:

  1. 50% fail

    Column is just a regular column; SQLAlchemy still sees no primary key.

  2. 80% fail

    Unique constraints don't satisfy the primary key requirement for ORM mapping.