documentation을 가지고 다음 표를 정의하고 채웠지 만 관계는 아직 정의되지 않았습니다.Reflection 후 SQLAlchemy에서 many to many 관계 추가하기
class CountryAssociation(Base):
__tablename__ = 'Country_Region_Mapping'
country_id = Column(Integer, ForeignKey('countries.uid'), primary_key=True)
region_id = Column(Integer, ForeignKey('regions.uid'), primary_key=True)
region = relationship('Region', back_populates='country')
country = relationship('Countries', back_populates='region')
extra_data = Column(String(50))
class Countries(Base):
__tablename__ = 'countries'
uid = Column(Integer, primary_key=True)
countryname = Column('English_short_name',
String(255), unique=True, nullable=False)
region = relationship('CountryAssociation',
back_populates='country')
class Region(Base):
__tablename__ = 'regions'
uid = Column(Integer, primary_key=True)
region = Column(String(255), unique=True, nullable=False)
country = relationship('CountryAssociation',
back_populates='region')
이제 테이블간에 많은 관계를 만들고 싶습니다. docs
Base = automap_base() #reflecting the orm way
engine = create_engine('sqlite:///mydatabse.db')
Base.prepare(engine, reflect=True)
Session = sessionmaker(bind=engine)
session = Session()
table_countries = Base.classes.countries
table_regions = Base.classes.regions
r = session.query(table_regions).filter(table_regions.region == "Western Europe").first()
c = session.query(table_countries).filter(table_countries.English_short_name == "Germany").first()
c.region.append(r) # this fails with
AttributeError은 '국가의 목적은 어떤 속성'지역 '이 그러나 작동
이 없습니다 : 내가 잘못 여기에 (초급) 뭐하는 거지 얻을 해달라고
c.countryname # Germany
을 ...
가 반사 ([협회 개체]와 http://docs.sqlalchemy.org/en/을 작동하지 않습니다 그것을 가능 rel_1_1/orm/basic_relationships.html # association-object)? – Bjoern
http://docs.sqlalchemy.org/ko/latest/orm/extensions/associationproxy.html#simplifying-association-objects를 보셨습니까? – plumSemPy
'c.countryname' 작업은 매우 이상하게 보입니다. 원래 클래스 정의를 만들 때'Base = automap_base()'를 사용했음을 알릴 수도 있습니다. –