alembic

Why is Flask-Migrate making me do a 2-steps migration?

时间秒杀一切 提交于 2019-12-06 02:05:14
问题 I'm working on a project with Flask, SQLAlchemy, Alembic and their wrappers for Flask (Flask-SQLAlchemy and Flask-Migrate). I have four migrations: 1c5f54d4aa34 -> 4250dfa822a4 (head), Feed: Countries 312c1d408043 -> 1c5f54d4aa34, Feed: Continents 41984a51dbb2 -> 312c1d408043, Basic Structure <base> -> 41984a51dbb2, Init Alembic When I start a new and clean database and try to run the migrations I get an error: vagrant@precise32:/vagrant$ python manage.py db upgrade ... sqlalchemy.exc

Trouble when using alembic with sqlalchemy_utils

和自甴很熟 提交于 2019-12-05 17:27:54
In my sqlalchemy model i use sqlalchemy_utils' choicetype: id = db.Column(db.Integer, primary_key=True) code = db.Column(db.Integer, nullable=True) level = db.Column(mytypes.types.ChoiceType(LEVEL)) I did everything as described here http://alembic.readthedocs.org/en/latest/autogenerate.html#autogen-module-prefix . In my model i imported choicetype from my module mytypes.types: from sqlalchemy_utils.types.choice import ChoiceType , in alembic/env.py i added context context.configure( connection=connection, target_metadata=target_metadata, user_module_prefix="mytypes.types." # ... ) , and in

Alembic: When autogenerating migrations how to ignore database tables by other products

爷,独闯天下 提交于 2019-12-05 00:51:28
I am using Alembic to manage migrations for a database. The same database is used by multiple Python packages and each of them have their own migration paths. How I can tell Alembic to ignore tables from other packages when generating automatic migrations? For example when I run: alembic -c development.ini revision --autogenerate -m "Initial migration" My migration Python file contains drop tables for other packages (not in the current Alembic env.py): def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.drop_table('table_from_another_package`) I can manually edit

.ini file load environment variable

你离开我真会死。 提交于 2019-12-05 00:11:08
I am using Alembic for migrations implementation in a Flask project. There is a alembic.ini file where the database configs must be specified: sqlalchemy.url = driver://user:password@host/dbname Is there a way to specify the parameters from the environment variables? I've tried to load them in this way $(env_var) but with no success. Thanks! I've solved the problem by setting sqlalchemy.url in env.py as @dirn suggested. config.set_main_option('sqlalchemy.url', <db_uri>) did the trick, where <db_uri> can be loaded from environment or config file. I was looking for a while how to manage this for

Relative importing python module from a subfolder from a different subfolder

扶醉桌前 提交于 2019-12-04 20:06:34
问题 I'm trying to use alembic which is an sqlalchemy tool in python. You type a command and it generates a folder "alembic" with py files inside. The py file inside, needs to link to my application in a separate folder called "myapp". But I cannot link it. It says it doesn't exist and relative import doesn't work. so I need to import my config class from myapp/configs/config.py file. /apps +--/alembic |----env.py <--- the calling file +--/myapp |----configs/__init__.py <--- has "DefaultConfig"

Alembic: How to migrate custom type in a model?

心已入冬 提交于 2019-12-04 19:00:24
问题 My User model is class User(UserMixin, db.Model): __tablename__ = 'users' # noinspection PyShadowingBuiltins uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True, unique=True) email = Column('email', String, nullable=False, unique=True) _password = Column('password', String, nullable=False) created_on = Column('created_on', sa.types.DateTime(timezone=True), default=datetime.utcnow()) last_login = Column('last_login', sa.types.DateTime(timezone=True), onupdate=datetime.utcnow())

Preserve existing tables in database when running Flask-Migrate

南笙酒味 提交于 2019-12-04 12:19:53
问题 I have a database with existing tables. My code has a User model. I generated a revision using Flask-Migrate and ran it, and it deleted my existing tables while creating the user table. How can I run migrations without removing the existing tables? from flask import Flask from flask_sqlalchemy import SQLAlchemy from flask_script import Manager from flask_migrate import Migrate, MigrateCommand app = Flask(__name__) app.config['SQLALCHEMY_DATABASE_URI'] = 'my_data' db = SQLAlchemy(app) migrate

Adding primary key to existing MySQL table in alembic

本秂侑毒 提交于 2019-12-04 01:09:57
问题 I am trying to add an 'id' primary key column to an already existing MySQL table using alembic. I tried the following... op.add_column('mytable', sa.Column('id', sa.Integer(), nullable=False)) op.alter_column('mytable', 'id', autoincrement=True, existing_type=sa.Integer(), existing_server_default=False, existing_nullable=False) but got the following error sqlalchemy.exc.OperationalError: (OperationalError) (1075, 'Incorrect table definition; there can be only one auto column and it must be

Run alembic upgrade migrations in a transaction

人盡茶涼 提交于 2019-12-04 00:40:38
问题 Does alembic upgrade head run inside a transaction so that all database alterations succeed, or fail? If not, why was it designed this way? 回答1: My understanding is alembic runs inside a transaction for databases that support it, like Postgres. If you're on a database that does not support this ( cough MySQL cough ), you can't use this functionality. 回答2: That's something you can decide within the env.py , which is where you customize the behaviour of the migrations to suit your setup. You

Alter Primary Key in Alembic?

…衆ロ難τιáo~ 提交于 2019-12-03 22:24:04
I've read through the docs , but I can't find instructions on this anywhere. I tried dropping the old key and adding a new one, but that gets me errors: op.drop_constraint('PRIMARY', 'some_table', type_='primary') op.create_primary_key('PRIMARY', 'some_table', ['col1', 'col2']) sqlalchemy.exc.OperationalError: (OperationalError) (1025, "Error on rename of ... (errno: 150 - Foreign key constraint is incorrectly formed)") 'ALTER TABLE some_table DROP PRIMARY KEY ' () What am I doing wrong? I also was in the same situation: alter primary key. In my case, I had to change the primary key type from