0

SQLAlchemy を使用して、テーブルを表すクラスを作成しました。最初のクラスから継承し、いくつかのメソッドをオーバーライドする 2 番目のクラスがあります。どのクラスを使用するかは、別のテーブルの値によって異なります。クエリを実行したり、関係プロパティにアクセスしたりするときに、SQLAlchemy が適切なクラスを作成するように、コードをどのように配置すればよいですか?

私のコードは次のようになります。

class Thing(Base):                                                                                                                                                                                             
    ...                                                                                                                                                                                                        
    relevant_property = Column(Bool)                                                                                                                                                                           


class RelatedThing(Base):  # Should be used if self.thing.relevant_property is True                                                                                                                            
    ...                                                                                                                                                                                                        
    thing = relationship("Thing")                                                                                                                                                                              
    def do_stuff(self):                                                                                                                                                                                        
        return 1                                                                                                                                                                                               


class RelatedThingChild(RelatedThing):  # Should be used if self.thing.relevant_property is False                                                                                                              
    def do_stuff(self):                                                                                                                                                                                        
        return 2                                                                                                                                                                                               


class SomethingDifferent(Base):                                                                                                                                                                                
    ...                                                                                                                                                                                                        
    related_thing = relationship("RelatedThing")  # Plugging in "RelatedThing" here probably isn't right, but I don't know what is                                                                             


def get_related_things():                                                                                                                                                                                      
    # This function doesn't know which version of RelatedThing it wants. Might even want a mix of both.                                                                                                        
    return DBSession.query(RelatedThing).all()  # Plugging in "RelatedThing" here probably isn't right, but I don't know what is                                                                               


def use_something_different(sd):                                                                                                                                                                               
    sd.related_thing.do_stuff()  # Needs sd.related_thing to be the right class.

更新:これは、関連するテーブルの実際の DB スキーマです。テーブルを表すクラスは、testsに基づいて変更する必要がありますseries.regression

pdiff=# \dS series
                                           Table "public.series"
     Column     |            Type             |                         Modifiers                          
----------------+-----------------------------+------------------------------------------------------------
 series_id      | integer                     | not null default nextval('series_series_id_seq'::regclass)
 name           | text                        | 
 control_domain | text                        | 
 test_domain    | text                        | 
 created        | timestamp without time zone | default now()
 reload_url     | text                        | 
 regression     | boolean                     | 
Indexes:
    "series_pkey" PRIMARY KEY, btree (series_id)
Referenced by:
    TABLE "tests" CONSTRAINT "tests_series_id_fkey" FOREIGN KEY (series_id) REFERENCES series(series_id)
    TABLE "urls" CONSTRAINT "urls_series_id_fkey" FOREIGN KEY (series_id) REFERENCES series(series_id)

pdiff=# \dS tests
                                       Table "public.tests"
  Column   |            Type             |                        Modifiers                        
-----------+-----------------------------+---------------------------------------------------------
 test_id   | integer                     | not null default nextval('tests_test_id_seq'::regclass)
 series_id | integer                     | 
 created   | timestamp without time zone | default now()
 status    | text                        | 
Indexes:
    "tests_pkey" PRIMARY KEY, btree (test_id)
Check constraints:
    "status_check" CHECK (status = ANY (ARRAY['complete'::text, 'running'::text, 'queued'::text, 'failed'::text]))
Foreign-key constraints:
    "tests_series_id_fkey" FOREIGN KEY (series_id) REFERENCES series(series_id)
Referenced by:
    TABLE "engine_tests" CONSTRAINT "diffs_test_id_fkey" FOREIGN KEY (test_id) REFERENCES tests(test_id)
    TABLE "urls_tests" CONSTRAINT "urls_tests_test_id_fkey" FOREIGN KEY (test_id) REFERENCES tests(test_id)
4

1 に答える 1