【发布时间】:2023-03-04 02:33:01
【问题描述】:
我有以下映射类,它是来自其他 2 个类的关联。
class InstanceCustomer(Base):
__tablename__ = 'Instances_Customers_Association'
cust_id = Column(Integer, ForeignKey('Customers.id'), primary_key=True)
inst_id = Column(Integer, ForeignKey('Instances.id'), primary_key=True)
customer = relationship(Customer, backref=backref('customer'))
instance = relationship(Instance, backref=backref('instance'))
def __init__(self, cust_id=None, inst_id=None):
self.cust_id = cust_id
self.inst_id = inst_id
def __repr__(self):
return "<InstanceCustomer(cust_id='%s', inst_id='%s')>" % (self.cust_id, self.inst_id)
我想将它与 Person 类相关联。因此,由于 1 InstanceCustomer 可以有多个 Person,而 1 Person 可以有多个 Instance Customer,我将需要它们之间的其他关联,我该怎么做?主键/外键也有问题吗?
这是 Person 类
class Person(Base):
__tablename__ = 'person'
id = Column( Integer, primary_key=True)
【问题讨论】:
标签: python sqlalchemy many-to-many foreign-key-relationship composite-primary-key