2017-12-10 5 views
0

documentationを定義すると、以下のテーブルが定義されていますが、まだリレーションが定義されていません。Reflectionの後にSQLAlchemyに多対多リレーションシップを追加する

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 

を...

+0

が、それは反射が[関連オブジェクト]で動作しないことは可能ですが(http://docs.sqlalchemy.org/en/ rel_1_1/orm/basic_relationships.html#association-object)? – Bjoern

+0

http://docs.sqlalchemy.org/en/latest/orm/extensions/associationproxy.html#simplifying-association-objectsを見たことがありますか? – plumSemPy

+0

'c.countryname'の動作は非常に奇妙で、クラス定義を元々作成しているときに' Base = automap_base() 'を使用したことを示唆するかもしれません。 –

答えて

0

association object patternextra_dataと使用したため、automap relationship detectionは認識されません。として:

  1. If the table contains two and exactly two ForeignKeyConstraint objects, and all columns within this table are members of these two ForeignKeyConstraint objects, the table is assumed to be a “secondary” table, and will not be mapped directly.

は別の方法置く:Country_Region_Mapping内にないすべての列をテーブルには、非常に多くの関係に何の多くが作成されず、ではありませんので、外部キー制約のメンバーです。

もう一度見落としたことはnaming schemeです。作業しているセカンダリテーブルがある場合、作成された関係はnamed regions_collection by default(複数のテーブル名のため)になります。

何が起こるんがregionsCountry_Region_Mapping、およびcountriesCountry_Region_Mappingの間には多くの関係への1/1の多くが作成されることである。

In [22]: table_countries.country_region_mapping_collection 
Out[22]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d600fb258> 

In [23]: table_regions.country_region_mapping_collection 
Out[23]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d600fbaf0> 

In [28]: table_country_region_mapping.countries 
Out[28]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d535a2990> 

In [29]: table_country_region_mapping.regions 
Out[29]: <sqlalchemy.orm.attributes.InstrumentedAttribute at 0x7f2d535a2938> 

注による複数のテーブルにスカラ関係がCountry_Region_Mappingに属性を命名することを複数の命名もあります。これらにより

心の中であなたがそうのような新しいアソシエーションを追加したい:

In [36]: c.country_region_mapping_collection.append(
    ...:  Base.classes.Country_Region_Mapping(countries=c, regions=r)) 
関連する問題