Вопрос : Как добавить условие ON для созданного объекта ассоциации (CustomerRoleAssociation), чтобы атрибут IsDisabled должен был быть равен 1?
Я попытался добавить аргумент primaryjoin для пользовательского условия, но, похоже, он не работает.
Цель : необходимо отфильтровать условие JOIN для атрибута объекта ассоциации (IsDisabled).
Ошибка : ArgumentError: Relationship CustomerRoleAssociation.Customer не смог определить однозначные пары локальных / удаленных столбцов на основе условия соединения и аргументов remote_side.Попробуйте использовать аннотацию remote () для точной маркировки тех элементов условия соединения, которые находятся на удаленной стороне отношения.
from sqlalchemy import Column,Integer,String,Table,ForeignKey, and_
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, foreign, remote
from sqlalchemy.ext.associationproxy import association_proxy
import logging
Base = declarative_base()
logger = logging.getLogger(__name__)
class MyModel():
def __init__(self):
self.customer = self.Customer()
self.role = self.Role()
logger.info("Model Initialized")
class CustomerRoleAssociation(Base):
__tablename__ = 'customer_role'
CustomerRoleId = Column(Integer, primary_key=True)
CustomerId = Column(Integer, ForeignKey('customer.CustomerId'))
RoleId = Column(Integer, ForeignKey('role.RoleId'))
IsDisabled = Column(Integer)
Customer = relationship("Customer",backref="role_associations",primaryjoin="and_(CustomerRoleAssociation.CustomerId==Customer.CustomerId,CustomerRoleAssociation.IsDisabled!=1)")
Role = relationship("Role", backref="customer_associations",primaryjoin="and_(CustomerRoleAssociation.CustomerId==Customer.CustomerId,CustomerRoleAssociation.IsDisabled!=1)")
class Customer(Base):
__tablename__ = 'customer'
CustomerId = Column(Integer, primary_key = True)
UserName = Column(String)
IsDisabled = Column(Integer)
IsDeleted = Column(Integer)
CustomerRoles = association_proxy("role_associations", "Role",
creator=lambda r: CustomerRoleAssociation(Role=r))
logger.info("Customer Model Initialized")
class Role(Base):
__tablename__ = 'role'
RoleId = Column(Integer, primary_key = True)
RoleName = Column(String)
IsDisabled = Column(Integer)
IsDeleted = Column(Integer)
RoleCustomers = association_proxy("customer_associations", "Customer",
creator=lambda c: CustomerRoleAssociation(Customer=c))
logger.info("Role Model Initialized")