I'm trying to connect Foreigner Database to a python-Flask
app using Flask_SQLALchemy
I looked everywhere including the FLASK_SQLALCHEMY official doc
I've been looking everywhere over the internet for the past 4 days for any tutorial that feature's FLASK_SQLALCHEMY
Library in ORM
without a luck
i kept looking over the SQLAlchemy Reflecting doc but I got confused on decide-ding what to do next
here is my flask code:
Base = automap_base()
xDB = 'postgres://****'
engine = db.create_engine(xDB)
metadata = db.MetaData()
Session = db.sessionmaker(bind=engine)
session = db.scoped_session(engine)
Base.metadata.reflect(engine)
y = [db.Table(table,metadata,autoload=True,autoload_with=engine) for
table in engine.table_names()]
I've tried to query in many different ways based on what I've read over many sources none of them worked with Flask_SQLAlchemy
attempt #1 :
t = db.select('test1').limit(10)
engine.execute(t).fetchall()
output :
t = SELECT test1.id, test1.name FROM test1 LIMIT :param_1
attempt #2 :
t = db.session.query([test1])
output:
sqlalchemy.exc.InvalidRequestError: SQL expression, column, or mapped entity expected - got '[Table('test1', MetaData(bind=None), Column('id', INTEGER(), table=, nullable=False, server_default=DefaultClause(, for_update=False)), Column('name', VARCHAR(), table=, nullable=False), schema=None)]'
i thought it's already mapped ..since autoLoad = True
, Base = automap_base()
and Base.metadata.reflect(engine)
attempt #3 :
t = metadata.tables['test1']
output:
KeyError: Table('test1', MetaData(bind=None), Column('id', INTEGER(), table=, nullable=False, server_default=DefaultClause(, for_update=False)), Column('name', VARCHAR(), table=, nullable=False), schema=None)
what i don't understand their metadata
is already defined up as metadata = db.MetaData()
I can't find anything for Flask_SQLAlchemy old or new but i can see some resources for SQLAlchemy that doesn't work for Flask_SQLAlchemy
Library , could someone help ?