alembic

How to run a migration with Python Alembic by code?

偶尔善良 提交于 2021-02-18 07:01:44
问题 I am trying to migrate a SQL database using sqlAlchemy and Alembic. I would like to make simple migration scripts directly in Python code, and not by using Alembic CLI, as described in the docs. I only found this SO question on this topic: Using Alembic API from inside application code Using this question + Flask-Alembic source code, I tried these simple commands. engine is linked to my db & metadata contains the information for the migration. I think I am very close, and the solution should

How to run a migration with Python Alembic by code?

痴心易碎 提交于 2021-02-18 06:59:22
问题 I am trying to migrate a SQL database using sqlAlchemy and Alembic. I would like to make simple migration scripts directly in Python code, and not by using Alembic CLI, as described in the docs. I only found this SO question on this topic: Using Alembic API from inside application code Using this question + Flask-Alembic source code, I tried these simple commands. engine is linked to my db & metadata contains the information for the migration. I think I am very close, and the solution should

Flask-migrate: change model attributes and rename corresponding database columns

折月煮酒 提交于 2021-02-10 12:38:07
问题 I have a bit of experience with Flask but not very much with databases (Flask-migrate / alembic / SqlAlchemy). I'm following this tutorial and things are working quite alright. I have a User model like this: # user_model.py from app import DB ... other imports class User(UserMixin, DB.Model): __tablename__ = 'users' id = DB.Column(DB.Integer, primary_key=True) username = DB.Column(DB.String(64), index=True, unique=True) email = DB.Column(DB.String(120), index=True, unique=True) password_hash

Flask-migrate: change model attributes and rename corresponding database columns

给你一囗甜甜゛ 提交于 2021-02-10 12:37:06
问题 I have a bit of experience with Flask but not very much with databases (Flask-migrate / alembic / SqlAlchemy). I'm following this tutorial and things are working quite alright. I have a User model like this: # user_model.py from app import DB ... other imports class User(UserMixin, DB.Model): __tablename__ = 'users' id = DB.Column(DB.Integer, primary_key=True) username = DB.Column(DB.String(64), index=True, unique=True) email = DB.Column(DB.String(120), index=True, unique=True) password_hash

Ignoring a model when using alembic autogenerate

浪子不回头ぞ 提交于 2021-02-10 05:21:46
问题 I am trying to autogenerate revisions for my DB using alembic . While doing so, I want to ignore some models (they have data types that are not supported by current version of MySQL). Here is what I tried and it seems to work fine, but I am not sure that's the most idiomatic way of doing it inside alembic/env.py def include_object(object, type_, name, reflected, compare_to): if type_ == 'table' and name == 'model_to_be_ignored': return False return True and then inside run_migrations_online

How can I initialize the database automatically with SQLalchemy and Alembic?

心不动则不痛 提交于 2021-02-09 08:48:09
问题 Currently, I run $ flask db init $ flask db migrate -m "initialization" $ flask db upgrade if the database does not exist. I would like to run this within Python, e.g. something like app.create_db() so that I don't have to care about setting the database up. Is that possible? I use the flask-sqlalchemy and flask-migrations plugins 回答1: Obviously, you have installed flask-migrate, flask-sqlalchemy . So, you can do like this: from flask_sqlalchemy import SQLAlchemy from flask import Flask app =

How to instantiate a table object to bulk_insert rows using alembic / SQLAlchemy

五迷三道 提交于 2021-02-08 14:03:52
问题 I am trying to use bulk_insert to insert data into an existing table ( services ) in my Postgres database. How do I instantiate this table object so I can do a bulk_insert with it? I saw answers like this: Alembic bulk_insert to table with schema but I want to avoid redefining the schema again in the migration. from alembic import op import sqlalchemy as sa from sqlalchemy.dialects import postgresql def upgrade(): """Up migration.""" services = sa.MetaData().Services() op.bulk_insert(services

Stored procedure with Alembic: MySQL Syntax Error

旧城冷巷雨未停 提交于 2021-01-28 18:38:05
问题 I am using Alembic to load a stored procedure into the MySQL database. Following the cookbook, I was able to create the required objects for creating and dropping the procedure. Now that I want to upgrade the version to actually load the procedure, I am getting SQL syntax near the DELIMITER $$ which I need for procedure definition. I have also tried to remove the DELIMITER and replace AS for starting the procedure, but that didn't work either. I even tried the simple example function in the

Perform alembic upgrade in multiple schemas

隐身守侯 提交于 2021-01-28 02:51:48
问题 I am using flask + sqlalchemy + alembic + postgresql, and am trying to find a simple architecture to solve the following problem. I have a simple database structure, lets say two tables : -- Users -- UserItems My users are in several different domains. I would like to have several of this database structure. For that I have database schemas in SQL. I created the matching class structure using sqlalchemy decalrative_base, and end up with a MetaData object that isn't tied to a specific schema.

How to use an existing sqlalchemy Enum in an Alembic migration (Postgres)

懵懂的女人 提交于 2021-01-27 13:00:53
问题 At some point in the past I've run an alembic migration which creates a users table like... def upgrade(): ... op.create_table( "users", sa.Column("id", sa.Integer(), autoincrement=True, nullable=False), ... sa.Column("type", sa.Enum("Foo", "Bar", "Baz", name="usertype"), nullable=False), ... ) ... ...which automatically creates the enum named usertype with the values "Foo", "Bar", "Baz" . Now, I want to make some other table which also references that same enum. e.g., def upgrade(): ... op