python - SQLALchemy 多态模型的多对多模型关系配置

标签 python sqlalchemy

因此,有几个问题和答案涉及到这个问题,但我无法将它们与我想要实现的目标完全一致。

Here , herehere

我有一组自引用和继承的模型。这是基本设计。

class BaseUser(db.Model):
    id = db.Column(db.Integer, primary_key=True, nullable=False)
    org = db.Column(db.Boolean, default=False, nullable=False)
    # Shared Fields
    __mapper_args__ = {
        'polymorphic_on': org,
    }

class Customer(BaseUser):
    # Customer Fields
    __mapper_args__ = {
        'polymorphic_identity': 0
    }

class Organization(BaseUser):
    # Organization Fields
    __mapper_args__ = {
        'polymorphic_identity': 1
    }

class CustomerOrganization(db.Model):
    user_id = db.Column(db.ForeignKey('customer.id', ondelete=CASCADE, onupdate=CASCADE), primary_key=True, nullable=False)
    org_id = db.Column(db.ForeignKey('customer.id', ondelete=CASCADE, onupdate=CASCADE), primary_key=True, nullable=False)

我尝试了几种不同的方法来为每种类型创建“组织”和“成员”关系。关于如何定义 relationsihp() 属性的任何建议?

最佳答案

可以使用 primaryjoinsecondaryjoin 属性来完成。相关文档为 here .

例子:

customer_organization = Table(
    'base_user_customer_organization', ModelBase.metadata,
    Column('user_id', Integer, ForeignKey('base_user.id')),
    Column('org_id', Integer, ForeignKey('base_user.id'))
)


class BaseUser(ModelBase):
    __tablename__ = 'base_user'

    id = Column(Integer, primary_key=True, nullable=False)
    org = Column(Boolean, default=False, nullable=False)
    # Shared Fields
    __mapper_args__ = {
        'polymorphic_on': org,
    }
    customers = relationship(
        "BaseUser",
        backref=backref('organization', order_by=id),
        secondary=customer_organization,
        primaryjoin=id==customer_organization.c.org_id and org==True,
        secondaryjoin=id==customer_organization.c.user_id and org==False
    )


class CustomerUser(BaseUser):
    # Customer Fields
    __mapper_args__ = {
        'polymorphic_identity': False
    }


class OrganizationUser(BaseUser):
    # Organization Fields
    __mapper_args__ = {
        'polymorphic_identity': True
    }

并测试:

sql = sqldb.get_session()
customer1 = sqldb.system.CustomerUser()
sql.add(customer1)
customer2 = sqldb.system.CustomerUser()
sql.add(customer2)
organization = sqldb.system.OrganizationUser()
organization.customers = [customer1, customer2]
sql.add(organization)
sql.commit()
# function prints all table data
print get_sql_table_data(sqldb.system.BaseUser)
print organization.customers
print customer1.organization
print customer2.organization

输出:

[{'org': False, 'id': 1}, {'org': False, 'id': 2}, {'org': True, 'id': 3}]
[<CustomerUser(id=1, org=False)>, <CustomerUser(id=2, org=False)>]
[<OrganizationUser(id=3, org=True)>]
[<OrganizationUser(id=3, org=True)>]

关于python - SQLALchemy 多态模型的多对多模型关系配置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34425777/

相关文章:

python - 如何在 QSystemTrayIcon 中左键单击时显示上下文菜单

python - 从列表中删除重复元素

python - 在python 3中查找表中名称第一个字符的频率分布

python - 声明性 SQLAlchemy 中的标签字典?

python - Geometry ('POINT' ) 列作为 str 对象返回

python - 按字母分割Python字符串并保留分隔符

python - django-pipeline 清除了 Django 数据库缓存中的条目

python - 如何使用 OAuth2 将 SQLAlchemy 连接到 Snowflake 数据库?

python - 如何在运行验证器时使用字典数据更新声明性 SQLAlchemy 模型

postgresql - SQLAlchemy 无法找到与非特权用户的外键关系