I-SQLAlchemy Database Migration yezicelo zeFlask zisebenzisa iAlembic

Ibhlog

I-SQLAlchemy Database Migration yezicelo zeFlask zisebenzisa iAlembic

I-SQLAlchemy Database Migration yezicelo zeFlask zisebenzisa iAlembic

Ukufuduswa kweFlask

I-Flask-Migrate lulwandiso olujongana nokufuduka kwedatha ye-SQLAlchemy yezicelo zeFlask zisebenzisa iAlembic. Imisebenzi yedatha ibonelelwa njengempikiswano yomgca wokuyalela phantsi kwe flask db umyalelo.



Ukufakwa

Faka iFlask-Migrate nge pip:

pip install Flask-Migrate

Umzekelo

Lo ngumzekelo wesicelo ophatha ukufuduswa kwedatha ngeFlask-Migrate:



from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_migrate import Migrate app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///app.db' db = SQLAlchemy(app) migrate = Migrate(app, db) class User(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(128))

Ngesicelo esingentla ungenza isiseko sedatha okanye wenze ukuba kube nokufuduka ukuba isiseko sedatha sele sikhona ngalo myalelo ulandelayo:

$ flask db init

Qaphela ukuba FLASK_APP Umahluko kwimeko-bume kufuneka usetwe ngokwamaxwebhu eFlask kulo myalelo ukuze usebenze. Oku kuyakongeza migrations ulawulo kwisicelo sakho. Imixholo yale folda kufuneka yongezwe kulawulo lwenguqulo kunye nezinye iifayile zomthombo.



Emva koko unokwenza imfuduko yokuqala:

$ flask db migrate

Iskripthi sokufuduka kufuneka siphononongwe kwaye sihlelwe, njengoko i-Alembic okwangoku ingayiboni yonke into oyenzayo kwiimodeli zakho. Ngokukodwa, i-Alembic okwangoku ayikwazi ukufumana izalathiso. Nje ukuba kugqitywe, iskripthi sokufuduka kufuneka songezwe kulawulo lwenguqulo.

Emva koko unokufaka isicelo sokufuduka kwindawo yogcino lwedatha:

$ flask db upgrade

Ke ngalo lonke ixesha iimodeli zedatha ziguqula ukuphinda migrate kunye upgrade imiyalelo.

Ukuvumelanisa isiseko sedatha kwenye inkqubo vele uvuselele migrations ulawulo kulawulo lwemithombo kwaye usebenzise upgrade umyalelo.

Ukubona yonke imiyalelo ekhoyo sebenzisa lo myalelo:

yintoni i-rune crypto
$ flask db --help

Izixhobo

Khuphela iinkcukacha:

Umbhali: miguelgrinberg
Khuphela ikhonkco: Khuphela iKhowudi yoMthombo
Iwebhusayithi esemthethweni: https://github.com/miguelgrinberg/Flask-Migrate
Ilayisensi: NGE

#database #sqlalchemy #flask #python #alembic