(venv) flask-tutorials % pip install flask-migrateĭownloading Flask_Migrate-3.0.1-py2.p圓-none-any.whl (12 kB) But the problem here is while developing, it happens to change the schema very frequently, so to make this transition simple, there are several good tools in the market that are dedicated to helping the transition schemas from one version to the next, these are called migration frameworks.įlask-migrateis a kind of framework that helps us to do the same migrations. To make this happen we need to provide the structured database schema to the ORM. The relational databases are very good at storing the structured content into the database. Now let’s create a database instance in _init_.py> from flask import FlaskĪpp.om_object(Config) # Making sure Config class available to app configurationĭb = SQLAlchemy(app) # Creating SQLAlchemy object bypassing the app SQLALCHEMY_DATABASE_URI = 'sqlite:///'+os.path.join(base_dir,'app.db')
In general, we used to define all our application configuration variables in a separate file called config.pyif you wish to do here you can freely define the database URI here also but make sure you have made this file to available to the flask app.config import osīase_dir = os.path.abspath(os.path.dirname(_file_)) On the above config, I have taken the app.db where all DB related stuff is going to maintain. After all, when we deploy the application into the production or any other major environments (DEV/QA), we can switch to MySQL or Postgress, etc., without making changes to our application.Īpp.config = 'sqlite:///app.db' The flask-sqlalchemy comes with the inbuilt SQLite database, so do not worry about the local database server. Successfully installed SQLAlchemy-1.4.21 flask-sqlalchemy-2.5.1 greenlet-1.1.0 Installing collected packages: greenlet, SQLAlchemy, flask-sqlalchemy Requirement already satisfied: MarkupSafe>=2.0 in. Requirement already satisfied: Werkzeug>=2.0 in. Requirement already satisfied: Jinja2>=3.0 in. Requirement already satisfied: click>=7.1.2 in. Requirement already satisfied: itsdangerous>=2.0 in. Requirement already satisfied: Flask>=0.10 in. (venv) flask-tutorials % pip install flask-sqlalchemyĭownloading Flask_SQLAlchemy-2.5.1-py2.p圓-none-any.whl (17 kB)
Head over to the virtual environment and run the install command. Install flask-sqlalchemy with the python package manager.