Flask (10) Use of Alembic, database structure management, upgrade, add table, add item

Source: Internet
Author: User
Tags virtual environment

1. Install the extension, install Alembic in the virtual environment, do not know can go to see Pycharm series articles.

2. Initialize, before using Alembic, you need to create a Alembic project with the Alembic init command that creates a Alembic.ini profile and a Alembic file directory (your_alembic_dir).

The General command is Alembic Init migrate

My Windows environment, and do not want to set so many environment variables, with Pycharm,

Refer to the following configuration:

File--settings--tools--external Tools

Create a new one yourself with the plus sign on the right.

Name is what you will see in the menu. If you want to underline, give a few more, an underscore, can't see clearly

Program: is the location of Alembic, this is the location of alembic in my virtual environment.

Parameters: is an optional command parameter.

Working directory: This is where the command is executed.

Group: You can have a name for your project, as a group exists in your menu. Sometimes it's not good to write multiple items and run wrong.

This is the effect you just set.

Try to run a little bit.

The Migrate directory and the Alembic.ini file are generated under the project folder

3. Configuration:

Modify Alembic.ini To configure the database connection.

Comment out the original, write on our own. The path starts with the project path, which is the working directory we set up

; sqlalchemy.url = driver://user:pass@localhost/=sqlite:///plan.db

Configure the migrate/env.py, configure the data model.

# Target_metadata = None Import OS Import syssys.path.append (Os.path.dirname (Os.path.abspath(__file__"/. /")fromimport= Base.metadata

Find your own path, and then introduce SQLAlchemy's base

4. Generate a script file to upgrade the database

Alembic Revision--autogenerate

5. Perform the upgrade

Alembic Upgrade Head

Both the sound script and the execution upgrade can be written into the pycharm extension according to the init command.

Flask (10) Use Alembic, database structure management, upgrade, add table, add item

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.