alembic

flask-migrate cannot drop table because other objects depend on it

大憨熊 提交于 2019-12-11 00:46:58
问题 Problem applying just created migration (Added db.Model) through Flask-Migrate (SQLAlchemy) for PostgresSQL DB. The error itself: sqlalchemy.exc.InternalError: (psycopg2.InternalError) cannot drop table parameter_subtype because other objects depend on it Full error stack trace is: INFO [alembic.autogenerate.compare] Detected removed foreign key (event_id) (id) on table stage_has_event INFO [alembic.autogenerate.compare] Detected removed column 'stage_has_event.event_id' Generating /app

alembic create_table, check if table exists

青春壹個敷衍的年華 提交于 2019-12-10 13:38:18
问题 I have an alembic upgrade script that creates a table, however I don't want it to create the table if it already exists. According to the alembic doc, I can pass in keyword args to op.create_tables that are acceptable to sqlalchemy.schema.table, so I'm using the keep_existing keyword: op.create_table('foo_model', sa.Column('foo_id', sa.Integer(), nullable=False), sa.Column('foo_str', sa.String(length=255), nullable=True), sa.PrimaryKeyConstraint('foo_id'), keep_existing= True ) However I'm

how to clean up incomplete alembic run

六眼飞鱼酱① 提交于 2019-12-10 13:03:14
问题 I'm trying to use alembic with a MySQL engine to perform online migrations. I've found that when an operation in my onupgrade() method fails my database gets stuck in an inconsistent state and i can't use alembic until I manually clean up any operations that happened before the failure in onupgrade() Example: def upgrade(): op.create_table('sometable', Column('id', INTEGER, primary_key=True), Column('name', VARCHAR(150), nullable=False, unique=True)) op.add_column('anothertable' Column('id',

.ini file load environment variable

℡╲_俬逩灬. 提交于 2019-12-10 01:14:01
问题 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! 回答1: 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,

Alter Primary Key in Alembic?

断了今生、忘了曾经 提交于 2019-12-09 14:05:57
问题 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? 回答1:

Alembic few modules to single database

此生再无相见时 提交于 2019-12-08 06:19:12
问题 We need to build a large application that split to multiple modules, App - Module1 --> AppDb - Module2 --> AppDb - Module3 --> AppDb Each module, we would like to add alembic version, all modules connect to single database. But we cannot add revision, because it's conflict to other module's revision (because all version store in same table alembic_version , so the migrations conflict with each other). 回答1: If you're using separate simple migrations in each of Module1 , Module2 and Module3

How can I ignore certain schemas with alembic --autogenerate

与世无争的帅哥 提交于 2019-12-07 08:42:55
问题 I have a library that's part of a bigger project. The library uses its own schema in a (PostgreSQL) database shared with the larger project. I want to use alembic revision --autogenerate to only generate migrations for the library's schema and ignore changes to the tables in the main/default schema. Is there any option for doing this? FWIW, I've tried the include_schemas=False parameter to context.configure in env.py, but it doesn't seem to do anything. 回答1: It seems I can use include_object

Flask-SQLAlchemy Lower Case Index - skipping functional, not supported by SQLAlchemy reflection

不打扰是莪最后的温柔 提交于 2019-12-07 08:10:52
问题 First off. Apologies if this has been answered but I can not find the answer any where. I need to define a lowercase index on a Flask-SQLAlchemy object. The problem I have is I need a models username and email fields to be stored lowercase so that I can check them with User.query.filter(func.lower(username) == func.lower(username)).first() Up to now I have always dealt with this by just inserting these fields as lowercase but in this particular instance I need the username to preserve the

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

半世苍凉 提交于 2019-12-06 19:40:02
问题 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

Run Alembic migrations on Google App Engine

[亡魂溺海] 提交于 2019-12-06 02:55:26
问题 I have a Flask app that uses SQLAlchemy (Flask-SQLAlchemy) and Alembic (Flask-Migrate). The app runs on Google App Engine. I want to use Google Cloud SQL. On my machine, I run python manage.py db upgrade to run my migrations against my local database. Since GAE does not allow arbitrary shell commands to be run, how do I run the migrations on it? 回答1: Whitelist your local machine's IP: https://console.cloud.google.com/sql/instances/INSTANCENAME/access-control/authorization?project=PROJECTNAME