대부분 SQLAlchemy에서 Client
모델을 가지고 있습니다. 주로 선언적입니다. 이 모델은 name
필드를 가지고 MetaphoneCode
모델과 다 대다 관계를 유지합니다. 코드는 다음과 같이 진행됩니다SQLAlchemy에서 다른 필드가 수정되었을 때 관계를 업데이트하는 방법
class MetaphoneCode(Base):
"""Represents a metaphone code
"""
__tablename__ = 'mcodes'
id = Column(Integer, Sequence('mcodes_seq_id'), primary_key=True)
code = Column(String(20), index=True, nullable=False)
client_mcode_assoc_table = Table(
'client_mcodes',
Base.metadata,
Column('client_id', ForeignKey('clients.id')),
Column('mcode_id', ForeignKey('mcodes.id')))
class Client(Base):
__tablename__ = 'clients'
id = Column(Integer, Sequence('client_id_seq'), primary_key=True)
name = Column(Unicode(100), index=True)
mcodes = relationship('MetaphoneCode', secondary=client_mcode_assoc_table)
을 이제 name
자체가 변경할 때 내가 client.name = "John Doe"
을 수행 할 때 내가 코드와 관련된 메타 폰을 계산하고 싶습니다 있도록 메타 폰 코드는 내가 이러한 요구를 표현하고 싶습니다 변경해야하기 때문에 그 이름 (성과 이름)이 업데이트되어 mcodes
관계가 업데이트되었습니다.
업데이트 : 간단한 해결책을 찾았습니다. below; 그래서 내 질문은 "으로 명시되어 있습니다. ORM 속성 이벤트 시스템이 다른 속성의 변경에 대한 응답으로 속성을 업데이트하는 최상의 방법입니까?"
InstrumentedAttribute를 통과하지 않으려면 왜해야하나요? – ScotchAndSoda
@ScotchAndSoda InstrumentedAttributed를 피하고 싶지는 않습니다. 단지 하위 클래스를 만들어서 그것의'__set__' 메쏘드가 콜백을 호출 할 것입니다. 하지만 SQLAlchemy에는 InstrutementedAttribute를 해킹하여 실제적으로 동일한 '@ event.listens_for'가 이미 있으므로 실제로는 필요하지 않습니다. 세터 용 +1 – manu