python - SQLAlchemy,同一张表上的一对一关系

标签 python sqlalchemy

我有一个位置类。位置可以具有默认的“帐单地址”,这也是位置。我正在使用的字段是 CustomerLocation 类中的 bill_to_idbill_to。为了完整起见,我已经将父类包含在内。如何将一个地点设置为另一地点的收单方?这种关系应该是一对一的(一个位置只能有一个收单方)。不需要 backref。

TIA

class Location(DeclarativeBase,TimeUserMixin):
    __tablename__ = 'locations'

    location_id = Column(Integer,primary_key=True,autoincrement=True)
    location_code = Column(Unicode(10))
    name = Column(Unicode(100))
    address_one = Column(Unicode(100))
    address_two = Column(Unicode(100))
    address_three = Column(Unicode(100))
    city = Column(Unicode(100))
    state_id = Column(Integer,ForeignKey('states.state_id'))
    state_relate = relation('State')
    zip_code = Column(Unicode(100))
    phone = Column(Unicode(100))
    fax = Column(Unicode(100))
    country_id = Column(Integer,ForeignKey('countries.country_id'))
    country_relate = relation('Country')
    contact = Column(Unicode(100))
    location_type = Column('type',Unicode(50))

    __mapper_args__ = {'polymorphic_on':location_type}

class CustomerLocation(Location):
    __mapper_args__ = {'polymorphic_identity':'customer'}
    customer_id = Column(Integer,ForeignKey('customers.customer_id',
                                            use_alter=True,name='fk_customer_id'))
    customer = relation('Customer',
                        backref=backref('locations'),
                        primaryjoin='Customer.customer_id == CustomerLocation.customer_id')
    tbred_ship_code = Column(Unicode(6))
    tbred_bill_to = Column(Unicode(6))
    ship_method_id = Column(Integer,ForeignKey('ship_methods.ship_method_id'))
    ship_method = relation('ShipMethod',primaryjoin='ShipMethod.ship_method_id == CustomerLocation.ship_method_id')
    residential = Column(Boolean,default=False,nullable=False)
    free_shipping = Column(Boolean,default=False,nullable=False)
    collect = Column(Boolean,default=False,nullable=False)
    third_party = Column(Boolean,default=False,nullable=False)
    shipping_account = Column(Unicode(50))
    bill_to_id = Column(Integer,ForeignKey('locations.location_id'))
    bill_to = relation('CustomerLocation',remote_side=['locations.location_id'])

最佳答案

查看我的answer to a related question 。通过在表中声明自引用外键,可以在声明式中拥有自引用关系,并且可以在声明类后立即对其进行“猴子修补”,或者将外来列名称指定为字符串而不是类字段。示例:

class Employee(Base):
  __tablename__ = 'employee'
  id = Column(Integer, primary_key=True)
  name = Column(String(64), nullable=False)
Employee.manager_id = Column(Integer, ForeignKey(Employee.id))
Employee.manager = relationship(Employee, backref='subordinates',
    remote_side=Employee.id)

我之前已经成功地使用过这种技术,它为您提供了父子树关系的两个方向(其中单个父记录可以有多个子记录)。如果您省略 backref 参数,它可能适合您,也可能不适合您。您始终可以简单地选择在应用程序中仅使用关系的一个方向。

关于python - SQLAlchemy,同一张表上的一对一关系,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9381536/

相关文章:

python - 使用 python 编辑打开的文档

python - 向 postgres 表中插入一个列表

python - 如何?时区 CURRENT_TIMESTAMP 'UTC'

python - 将 `null` 视为表唯一约束中的不同值

python - SqlAlchemy:如何在 where 子句中使用选定子查询的结果

python - pytest fixture 如何更改 fixture 范围

python - “TopLevelDocumentMetaclass”对象不可迭代

python - 使用pydot在Graphviz中垂直放置节点

python - 将 postgres "time with timezone"与 sqlalchemy 一起使用

python - 炼金术;外键错误