代码之家  ›  专栏  ›  技术社区  ›  Michael Wales

Python SQLAlchemy/Elixer问题

  •  0
  • Michael Wales  · 技术社区  · 16 年前

    我试图定义一个SQLAlchemy/Elixer模型,它可以描述以下关系。我有一个SSP表,它有多个POC表的外键。我已经在SSP对象中正确定义了多通关系(允许我 SSP.get(1).action.first_name 正确)。我还想补充的是这种关系的另一面,在那里我可以执行类似的操作 POC.get(1).csa 并返回其中此POC定义为idPOCCSA的SSP对象列表。

    我知道这对多态关联来说是最好的,但我真的根本无法更改DB模式(创建一个新的poc2ssp表,其中包含一列 type (协会的成员)。

    class POC(Entity):
      using_options(tablename = 'poc', autoload = True)
    
      # These two line visually display my "issue":
      # csa = OneToMany('SSP')
      # action = OneToMany('SSP')
    
    
    class SSP(Entity):
      '''
      Many to One Relationships:
      - csa: ssp.idPOCCSA = poc.id
      - action: ssp.idPOCAction = poc.id
      - super: ssp.idSuper = poc.id
      '''
      using_options(tablename = 'spp', autoload = True)
    
      csa = ManyToOne('POC', colname = 'idPOCCSA')
      action = ManyToOne('POC', colname = 'idPOCAction')
      super = ManyToOne('POC', colname = 'idPOCSuper')
    

    1 回复  |  直到 13 年前
        1
  •  1
  •   Vinay Sajip    16 年前

    请尝试以下操作:

    class POC(Entity):
      # ...
      #declare the one-to-many relationships
      csas = OneToMany('SSP')
      actions = OneToMany('SSP')
      # ...
    
    class SSP(Entity):
      # ...
      #Tell Elixir how to disambiguate POC/SSP relationships by specifying
      #the inverse explicitly.
      csa = ManyToOne('POC', colname = 'idPOCCSA', inverse='csas')
      action = ManyToOne('POC', colname = 'idPOCAction', inverse='actions')
      # ...