I have the following structure :
import sqlalchemy as sa
import sqlalchemy.orm as orm
Base = orm.declarative_base()
class Equity(Base) :
__tablename__ = 'equities'
id = sa.Column(sa.String, primary_key=True)
name = sa.Column(sa.String, nullable=False)
currency = sa.Column(sa.String, nullable=False)
country = sa.Column(sa.String, nullable=False)
sector = sa.Column(sa.String, nullable=True)
def __repr__(self) :
return f"Equity('Ticker: {self.id}', 'Name: {self.name}')"
class Bond(Base) :
__tablename__ = 'bonds'
id = sa.Column(sa.String, primary_key=True)
name = sa.Column(sa.String, nullable=False)
country = sa.Column(sa.String, nullable=False)
currency = sa.Column(sa.String, nullable=False)
sector = sa.Column(sa.String, nullable=False)
def __repr__(self) :
return f"Bond('Ticker: {self.id}', 'Name: {self.name}')"
And I want to create a new permanent table inside the database that is the UNION of those two tables (using the columns ID and CURRENCY)
I know I can create that outside using this :
results = session.query(Equity.id, Equity.currency).union(session.query(Bond.id, Bond.currency))
But I want (if possible) to have a relationship table inside my Database that automatically updates when I change anything on either on the EQUITIES or BONDS table. Something like this :
class NewUnionTable(Base) :
<relationship><union>
Can someone help me create this, please? Appreciate very much