Dynamic FK using SQLAlchemy ORM -


in data_id field of product_data table stored data records id's of bio_data, geo_data or phy_data according field datatable_name of product table prod_id.

enter image description here

so, need that, taking account name of table stored in datatable_name field (bio_data, geo_data or phy_data), data_id field in product_data point correspondent table records.

how can define relationships using sqlalchemy orm?

my approach this:

t_product_data = table(     'product_data', metadata,     column('prod_id', foreignkey(u'product.prod_id'), primary_key=true, nullable=false),     column('data_id', foreignkey(u'bio_data.bio_id'), primary_key=true, nullable=false),     schema='public' )  class product(base):     __tablename__ = 'product'     __table_args__ = {u'schema': 'public'}      datatable_name = column(string)     prod_id = column(integer, primary_key=true)      data = relationship(u'bio_data', secondary=t_product_data) 

this works fine, however, can see declaration static , works bio_data example. need put work other data sources (geo_data , phy_data), don't know, maybe making use of datatable_name field variable, this:

class product(base):     __tablename__ = 'product'     __table_args__ = {u'schema': 'public'}      datatable_name = column(string)     prod_id = column(integer, primary_key=true)      data = relationship(datatable_name, secondary=t_product_data) 

and should fk data_id in product_data table? possible create dynamic fk, points different tables according value of field product.datatable_name?


Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -