Pandas Dataframe to_sql with Flask-SQLAlchemy

2.6k Views Asked by At

I am working with two csv files that i have merged into one dataframe that i am currently storing as an sql databse using pandas to_sql(). I am writing all my app with Flask and i would like to use SQLAlchemy. So far i have created the sql database like this:

df.to_sql("User", con=db.engine, if_exists="append")

The problem now is that i would like to keep using SQLAlchemy 'syntax' to do queries and to use features like pagination. This is how it should be if i wanted to execute a query:

users = User.query.all().paginate(...)

However i haven't created in my models.py my User table since when doing it with pandas to_sql it will automatically create the table into the database for me. But now since i don't have my table defined in my models.py file i don't know how can i define my global variable 'Users' so i can proceed to use the query and other SQLAlchemy methods.

Thank you,

0

There are 0 best solutions below