我在我的应用程序中使用SQLAlchemy来编辑 PostgreSQL 数据库中的客户信息。 现有数据模型是
Table "customers"
customer | name | address
---------|------------|--------
1 | customer 1 | xxx
2 | customer 2 | yyy
Table "customers_data"
customer | key | value
---------|--------------------|----------
1 | birthdate | 1.1.2000
1 | number_of_children | 2
2 | birthdate | 2.2.2002
2 | number_of_children | 1
customer_data.customer is foreign key
我想要一个 SQLAlchemy 映射类
class Customer(Base):
__tablename__ = 'customers'
customer = Column(VARCHAR, primary_key=True)
name = Column(TEXT)
address = Column(TEXT)
# additional fields
birthdate = ...
number_of_children = ...
映射其他字段(birthdate,number_of_children,...)的最佳方法是什么? 我不确定如何在 SQLAlchemy 中执行此操作。