Существуют ли возможности для создания представлений путем объединения двух таблиц в алхимии:
class CreateView(Executable, ClauseElement):
def __init__(self, name, select):
self.name = name
self.select = select
@compiles(CreateView)
def visit_create_view(element, compiler, **kw):
return "CREATE VIEW %s AS %s" % (
element.name,
compiler.process(element.select, literal_binds=True)
)
# Method to create view with top three articles
def view_top_three():
top_three_view = CreateView('test', db.select([art.columns.title, func.count(log.columns.path)])
.group_by(policies.columns.name, join_object.columns.policy_name) \
# engine.execute(top_three_view)
v = Table('popular', metadata, autoload=True, autoload_with=engine)
for r in engine.execute(v.select()):
print(r)
def main():
try:
engine = db.create_engine(DATABASE_URI).execution_options(autocommit=True)
connection = engine.connect()
except:
print('Error establishing DB connection')
# Import metadata
metadata = db.MetaData()
# Import articles, authors and log tables
art = db.Table('policies', metadata, autoload=True, autoload_with=engine)
aut = db.Table('join_object', metadata, autoload=True, autoload_with=engine)
# Call the method which creates view and selects from view
view_top_three()