python type_error ai_generated true

sqlalchemy.exc.ArgumentError: 类型对象 'UUID' 不是有效的 SQLAlchemy 类型

sqlalchemy.exc.ArgumentError: Type object 'UUID' is not a valid SQLAlchemy type

ID: python/sqlalchemy-unknown-column-type

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

版本兼容性

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

根因分析

在 Column 定义中直接使用 Python 类型如 uuid.UUID,而没有使用 SQLAlchemy 类型适配器。

English

Using a Python type like uuid.UUID directly in a Column definition without a SQLAlchemy type adapter.

generic

解决方案

  1. 90% 成功率
    Use sqlalchemy.dialects.postgresql.UUID for PostgreSQL: from sqlalchemy.dialects.postgresql import UUID; id = Column(UUID(as_uuid=True), primary_key=True)
  2. 90% 成功率
    Use sqlalchemy.types.UUID (available in SQLAlchemy 2.0): from sqlalchemy import UUID; id = Column(UUID(as_uuid=True))

无效尝试

常见但无效的做法:

  1. 100% 失败

    SQLAlchemy doesn't automatically map Python's uuid.UUID.

  2. 70% 失败

    ImportError occurs if not imported properly.