Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create classes from existing tables using Flask-SQLaclhemy

I know I need to use the MetaData object, in SQLAlchemy, but I am not sure how to use it with a class,

db = SQLAlchemy(app)
meta =db.Metadata()
class orders(db.model):
  pass

How do I pass the meta object to the class so that it will auto generate table schema?

like image 716
LeoG Avatar asked Apr 05 '15 08:04

LeoG


People also ask

How do you create a table using SQLAlchemy in Flask?

Step 1 - Install the Flask-SQLAlchemy extension. Step 2 - You need to import the SQLAlchemy class from this module. Step 3 - Now create a Flask application object and set the URI for the database to use. Step 4 - then use the application object as a parameter to create an object of class SQLAlchemy.

How do you update existing table rows in SQLAlchemy in Python?

Update table elements in SQLAlchemy. Get the books to table from the Metadata object initialized while connecting to the database. Pass the update query to the execute() function and get all the results using fetchall() function. Use a for loop to iterate through the results.

How do you use Flask-SQLAlchemy to interact with databases in a Flask application?

You then create a Flask application instance called app , which you use to configure two Flask-SQLAlchemy configuration keys: SQLALCHEMY_DATABASE_URI : The database URI to specify the database you want to establish a connection with. In this case, the URI follows the format sqlite:/// path/to/database. db .

What is difference between Flask-SQLAlchemy and SQLAlchemy?

One of which is that Flask-SQLAlchemy has its own API. This adds complexity by having its different methods for ORM queries and models separate from the SQLAlchemy API. Another disadvantage is that Flask-SQLAlchemy makes using the database outside of a Flask context difficult.


2 Answers

Well you can use SQLAlchemy's autoload feature but I still haven't figured out how to use that from flask-sqlalchemy. Here's a tutorial if you want to read about it anyway: SQLAlchemy Connecting to pre-existing databases. The best solution I found for the time being is to use sqlautocode to generate the SQLAlchemy models from the existing tables in your database. I know it would be preferable if SQLAlchemy would handle that automatically but I can't find a way to do it from Flask.

Here's how to use it:

sqlautocode mysql://<dbuser>:<pass>@localhost:3306/<dbname> -o alchemy_models.py

This will generate the Models and place them in the alchemy_models.py file. I hope this helps

like image 139
Codejunky Avatar answered Nov 13 '22 05:11

Codejunky


You can use sqlacodegen to generate the classes needed for sqlalchemy.

pip install sqlacodegen

sqlacodegen postgresql+psycopg2://username:password@host/database --outfile models.py

I ran into an issue with the Base class and the query attribute. The error I received was:

AttributeError: type object 'PaymentType' has no attribute 'query' 

I was able to make the sqlacodegen classes work by using a scoped_session.

session = scoped_session(sessionmaker(autocommit=False,autoflush=False,bind=engine))
Base.query = session.query_property()
print(PaymentType.query.all())
like image 3
VERNSTOKED Avatar answered Nov 13 '22 04:11

VERNSTOKED