python - 尝试将对象属性映射到不同名称的数据库列
问题描述
我是一个新用户,正在 Python 中使用 SQLAlchemy 和 mysql。我有一个映射到客户表的客户对象。我决定以不同于数据库中列名的方式命名对象属性。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'
难道我不能这样做。我在类中的属性是被映射到列 firstName 的 first_name。
任何帮助将不胜感激。
解决方案
I am sorry. I had a typo in another part of my code which printed a retrieved object which was causing this error. My apologies.
推荐阅读
- python - 从标准输出打开读取需要很长时间
- sql - 从特定时间之间的多天选择数据(跨越 2 天)
- aws-lambda - 有什么方法可以从 AWS lambda(Node JS)禁用 *Alexa 卡*?
- python - 将 np.float64 对象除以 2 会使返回值的 dtype 为 np.float32 吗?
- javascript - 我无法使用函数通过 DOM 编辑(拼接)我的数组中的特定索引
- ios - iOS App with Swift5 与 ios 10.3 版本不兼容
- java - RabbitMQ - 只有一个队列,多个消费者接收不同的消息
- c - 何时需要函数原型中的常量指针(而不是指向常量的指针)才能使代码正确?
- spring-data-jpa - 如何使用 spring jpa 运行联合查询?
- javascript - 单击按钮后再次启动 setInterval