尝试将对象属性映射到另一个名为

2024-10-01 04:53:13 发布

您现在位置:Python中文网/ 问答频道 /正文

我是一个新用户,正在Python中使用SQLAlchemy和mysql。我有一个映射到customer表的customer对象。我决定将对象属性命名为与数据库中的列名不同的名称。SQLAlchemy 1.3.11的文档表明这是允许的

我的班级看起来像这样

class Customer(declarative_base(), PersistentBase.PersistentBase):
    __tablename__ = 'customers'

    id = Column(BigInteger, primary_key=True, autoincrement=True)
    first_name = Column('firstName', String(35), nullable=False)
    last_name = Column('lastName', String(35), nullable=False)

当我尝试执行某些操作(如检索)时,会出现以下错误:

AttributeError: 'Customer' object has no attribute 'firstName'

我不应该这样做。我在类中的属性是first_name,它映射到一列firstName

任何帮助都将不胜感激


Tags: 对象用户namefalsetruestring属性sqlalchemy
1条回答
网友
1楼 · 发布于 2024-10-01 04:53:13

对不起。我的代码的另一部分有一个打字错误,它打印了一个检索到的对象,这导致了这个错误。我道歉

相关问题 更多 >