【问题标题】:AttributeError: 'UUID' object has no attribute 'replace'AttributeError:“UUID”对象没有“替换”属性
【发布时间】:2020-03-23 17:29:42
【问题描述】:

作为参考,我一直主要使用这两个资源

AttributeError: 'UUID' object has no attribute 'replace' when using backend-agnostic GUID type https://websauna.org/docs/narrative/modelling/models.html#uuid-primary-keys

这似乎表明问题已解决,但我似乎无法让它在我的最终工作。

我的错误与上面的 SO 帖子中的相同,但为了完整起见,这里是:

 File "/Library/Frameworks/Python.framework/Versions/3.5/lib/python3.5/uuid.py", line 137, in __init__
    hex = hex.replace('urn:', '').replace('uuid:', '')
AttributeError: 'UUID' object has no attribute 'replace'

我的模型如下:

import sqlalchemy
from .base import Base
from sqlalchemy import Column
from sqlalchemy.dialects.postgresql import UUID

class ModelItem(Base):
    __tablename__ = 'item'

    id = Column(UUID(as_uuid=True),
            server_default=sqlalchemy.text("uuid_generate_v4()"), 
            primary_key=True, 
            nullable=False)

还有其他已知的解决方法吗?

【问题讨论】:

  • 尝试将hex变量转换成字符串,然后执行替换任务。
  • 我该怎么做?它是在库中完成的,所以我会进入模块并在那里切换代码吗?这似乎很危险
  • 你是如何触发这个错误的?我无法使用 sqlalchemy 1.3.10、psycopg2 2.8.4、python 3.8 从您的代码中重现它。

标签: python postgresql flask sqlalchemy


【解决方案1】:

hex = str(hex).replace('urn:', '').replace('uuid:', '')

【讨论】:

  • 我应该对这在未来引起错误有什么程度的关注?乍一看看起来不错,但不确定它实际上会产生什么后果
  • 我觉得它不会产生任何不良后果,因为我们只是对变量进行类型转换并将其存储到同一变量的新实例中。我们不会在此处修改实际功能或任何内容。为了更安全,存储输出是一个新变量,例如: new_hex = str(hex).replace('urn:', '').replace('uuid:', '')
猜你喜欢
  • 1970-01-01
  • 2021-12-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-12-01
  • 2021-04-19
  • 1970-01-01
  • 2021-11-22
相关资源
最近更新 更多