python config_error ai_generated true

无法为映射表 'orders' 组装任何主键列

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

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

其他格式: JSON · Markdown 中文 · English
80%修复率
90%置信度
0证据数
2024-05-18首次发现

版本兼容性

版本状态引入弃用备注
3.x active

根因分析

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

English

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

解决方案

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

无效尝试

常见但无效的做法:

  1. 50% 失败

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

  2. 80% 失败

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