Flask-Migrate | SQLAlchemy database migrations for Flask applications | Data Migration library
kandi X-RAY | Flask-Migrate Summary
kandi X-RAY | Flask-Migrate Summary
Flask-Migrate is an extension that handles SQLAlchemy database migrations for Flask applications using Alembic. The database operations are provided as command-line arguments under the flask db command.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
- Run migrations on the engine
- Get metadata for a bind
- Return a config object
- Call all callbacks
- List available templates
- Returns the template directory
- Run migrations
- Run migrations on the database
Flask-Migrate Key Features
Flask-Migrate Examples and Code Snippets
Command used for migration
flask db migrate
This does the initial migration and creates the relevant files
flask db upgrade
This finally upgrades your database
Command used for downgrade
flask db downgrade
Community Discussions
Trending Discussions on Flask-Migrate
QUESTION
Is there any way to skip a bind using flask db migrate / alembic?
I have two repos which have SQLALCHEMY_BINDS which use a common database, but otherwise the databases are different. In my case members repo uses db members
, SQLALCHEMY_BINDS={'users': usersdb_uri}
and contracts repo uses db contracts
, SQLALCHEMY_BINDS={'users': usersdb_uri}
.
I want the members repo to handle migrations of the users
db, and contracts repo to ignore it for database migration.
I'm trying to use flask-migrate to do the initial migration to add the users bind to the contracts repo, which requires some changes to the contracts
db
In the contracts repo I tried modifying alembic's env.py to pop the users bind from SQLALCHEMY_BINDS
...ANSWER
Answered 2022-Mar-31 at 10:42The pop()
call that you are doing is messing up your Flask configuration. Instead of going at it this way, I recommend that you use the include_object from Alembic to have it skip the tables you don't want to migrate.
QUESTION
I am trying to get a Flask and Docker application to work but when I try and run it using my docker-compose up
command in my Visual Studio terminal, it gives me an ImportError called ImportError: cannot import name 'json' from itsdangerous
. I have tried to look for possible solutions to this problem but as of right now there are not many on here or anywhere else. The only two solutions I could find are to change the current installation of MarkupSafe and itsdangerous to a higher version: https://serverfault.com/questions/1094062/from-itsdangerous-import-json-as-json-importerror-cannot-import-name-json-fr and another one on GitHub that tells me to essentially change the MarkUpSafe and itsdangerous installation again https://github.com/aws/aws-sam-cli/issues/3661, I have also tried to make a virtual environment named veganetworkscriptenv
to install the packages but that has also failed as well. I am currently using Flask 2.0.0 and Docker 5.0.0 and the error occurs on line eight in vegamain.py.
Here is the full ImportError that I get when I try and run the program:
...ANSWER
Answered 2022-Feb-20 at 12:31I was facing the same issue while running docker containers with flask.
I downgraded Flask
to 1.1.4
and markupsafe
to 2.0.1
which solved my issue.
Check this for reference.
QUESTION
I've been struggling with this issue on and off for quite some time, and strangely could not find a straightforward question/answer combo on this on SO. Related questions here and here. I finally found a solution so I will ask and answer my own question.
In Flask SQLAlchemy (and regular SQLAlchemy), you can have a column like this:
...ANSWER
Answered 2022-Mar-12 at 17:50There is an answer to this question in the best practices suggested here, at the end of the section on The Importance of Naming Conventions. The solution is to add a naming_convention
to your sqlalchemy
metadata, like this:
QUESTION
I am trying to migrate my python changes to my docker project, however when I try to run the command python manage.py db migrate
I get the error unknown database 'main'
, however when I look inside of my docker-compose.yml file I see that main is indeed defined inside of the MYSQL_DATABASE
variable inside of the container db. I have tried some solutions found on StackOverflow as well as Github like getting the restart: always
out of my docker-compose.yml script and making a Windows PowerShell script that will run to restart my docker container as found here: MYSQL Docker container gives "unknown database" error, and trying to change my DATA_SAVE_PATH
variable and other such variables in my docker-compose.yml: https://github.com/laradock/laradock/issues/1017 and I have also tried to change the MYSQL_DATABASE
variable to a different name but that doesn't work either.
Here is the full error that I am receiving when I run my code:
...ANSWER
Answered 2022-Mar-02 at 19:43I solved this problem by looking in my .dbdata folder and found my service, I then changed the MYSQL_DATABASE
variable and the app configuration to the same variable as MYSQL_DATABASE
which was vegatest
instead of main
.
So I ran the same command python manager.py db migrate
and I got a successful migration.
QUESTION
I'm working on rewriting the test suite for a large application using pytest
and looking to have isolation between each test function. What I've noticed is, multiple calls to commit
inside a SAVEPOINT are causing records to be entered into the DB. I've distilled out as much code as possible for the following example:
init.py
...ANSWER
Answered 2022-Feb-23 at 17:04With the help of SQLAlchemy's Gitter community I was able to solve this. There were two issues that needed solving:
- The
after_transaction_end
event was being registered for each individual test but not removed after the test ended. Because of this multiple events were being invoked between each test. - The
_db
being yielded from thedb
fixture was inside the app context, which it shouldn't have been.
Updated conftest.py
:
QUESTION
I've tried simply removing the unique=True
constraint and running
ANSWER
Answered 2022-Jan-04 at 12:27Flask-Migrate, or rather Alembic, will not autodetect anonymous constraints. But you can create a manual Alembic migration file to drop the constraint.
Create an empty migration file:
QUESTION
I get this error when I type docker-compose build
on Ubuntu-20.04
WSL
ANSWER
Answered 2021-Dec-06 at 13:28Remove the prefix of the directory before setup.py
. Change your Dockerfile to:
QUESTION
I'm a python main. I started node express project and I'm confused with commands that sequelize use for migrations.
In python flask-migrate, you write your model then doing flask db migrate
makes a migration according to your model you've written. Then to apply your model to DB, you type flask db upgrade
command.
But I've found that in Node.js, sequelize-cli commands are meant different. sequelize db:migrate
actually "apply" existing migration which corresponds to upgrade
command in python flask.
My questions is, then what is correct procedure to define or modify your model in Node.js? You have to make your model class "AND" migration script yourself then run db:migrate
command?
It seems weird that I have to make same changes twice (Model & migration script) would there be another simple way?
...ANSWER
Answered 2021-Nov-30 at 15:35You can avoid doing this "double work" if you bypass migrations entirely and use the sequelize.sync() method, which builds the tables from your models.
The downside of this is that when you alter your models, you will need to pass in the { force: true } flag into the .sync() method in order for the new/altered attributes to be included. This option drops your tables and rebuilds them. This is fine for development, but awkward for production.
QUESTION
pip freeze output:
aiohttp==3.8.1
aiosignal==1.2.0
alembic==1.7.5
aniso8601==9.0.1
async-timeout==4.0.1
attrs==21.2.0
base58==2.1.1
bitarray==1.2.2
certifi==2021.10.8
charset-normalizer==2.0.7
click==8.0.3
cytoolz==0.11.2
eth-abi==2.1.1
eth-account==0.5.6
eth-hash==0.3.2
eth-keyfile==0.5.1
eth-keys==0.3.3
eth-rlp==0.2.1
eth-typing==2.2.2
eth-utils==1.10.0
Flask==2.0.2
flask-marshmallow==0.14.0
Flask-Migrate==3.1.0
Flask-RESTful==0.3.9
Flask-Script==2.0.6
Flask-SQLAlchemy==2.5.1
frozenlist==1.2.0
hexbytes==0.2.2
idna==3.3
ipfshttpclient==0.8.0a2
itsdangerous==2.0.1
Jinja2==3.0.3
jsonschema==3.2.0
lru-dict==1.1.7
Mako==1.1.6
MarkupSafe==2.0.1
marshmallow==3.14.1
marshmallow-sqlalchemy==0.26.1
multiaddr==0.0.9
multidict==5.2.0
netaddr==0.8.0
parsimonious==0.8.1
protobuf==3.19.1
psycopg2==2.9.2
pycryptodome==3.11.0
pyrsistent==0.18.0
pytz==2021.3
requests==2.26.0
rlp==2.0.1
six==1.16.0
SQLAlchemy==1.4.27
toolz==0.11.2
typing_extensions==4.0.0
urllib3==1.26.7
varint==1.0.2
web3==5.25.0
websockets==9.1
Werkzeug==2.0.2
yarl==1.7.2
...ANSWER
Answered 2021-Nov-22 at 00:29Are you sourcing your venv before running test.py?
If so, then try this,
QUESTION
I am trying to deplow a django project to heroku but i am getting this error.
ERROR: Command errored out with exit status 1: /app/.heroku/python/bin/python /app/.heroku/python/lib/python3.8/site-packages/pip/_vendor/pep517/_in_process.py prepare_metadata_for_build_wheel /tmp/tmp5mrz1adn Check the logs for full command output. ! Push rejected, failed to compile Python app. ! Push failed
This is the entire log
...ANSWER
Answered 2021-Oct-29 at 11:41After carefully reading log file, here's what I recommend to do for clean deploy.
While venv is activated and you're in project directory (where manage.py
live), do the following:
- Open requirements.txt
- Delete unneeded modules from requirements.txt
- Uninstall unneeded modules with
pip uninstall module
- Now run
pip freeze requirements.txt
- Deactivate venv
- Then add
requirements.txt
to source controlgit add -A
- Commit your changes
git commit -m "deleted unneeded modules"
- If you have existed Heroku app, connect to it with
heroku git:remote -a myapp
, else pass this step - Deploy,
git push heroku master
If you are unsure which modules your project need and I can't determin exactly the project's requirements but if you have basic project, you only need 3 modules which are Django
, psycopg2-binary
, gunicorn
, so do the following for clean deploy:
- Delete venv
- Outside project directory, create new one
- Activate venv
- Install the modules you need, probably
Django
,psycopg2-binary
,gunicorn
- Now run
pip freeze requirements.txt
- Deactivate venv
- Then add
requirements.txt
to source controlgit add -A
- Commit your changes
git commit -m "deleted unneeded modules"
- If you have existed Heroku app, connect to it with
heroku git:remote -a myapp
, else pass this step - Deploy,
git push heroku master
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Flask-Migrate
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page