alembic

How do I get alembic to emit custom DDL on after_create?

蹲街弑〆低调 提交于 2019-12-03 19:28:41
问题 I've got a couple of custom DDL statements that I want to run after create table: update_function = DDL(""" CREATE OR REPLACE FUNCTION update_timestamp() RETURNS TRIGGER AS $$ BEGIN NEW.updated_at = now(); RETURN NEW; END; $$ language 'pgplsql'; """) update_trigger = DDL(""" CREATE TRIGGER update %(table)s_timestamp BEFORE UPDATE ON %(table)s FOR EACH ROW EXECUTE PROCEDURE update_timestamp(); """) And I've attached them like this: event.listen(Session.__table__, 'after_create', update

sqlalchemy: alembic bulk insert fails: 'str' object has no attribute '_autoincrement_column'

社会主义新天地 提交于 2019-12-03 18:07:40
问题 My model looks like class Category(UserMixin, db.Model): __tablename__ = 'categories' uuid = Column('uuid', GUID(), default=uuid.uuid4, primary_key=True, unique=True) name = Column('name', String, nullable=False) parent = Column('parent', String, nullable=False) created_on = Column('created_on', sa.types.DateTime(timezone=True), default=datetime.utcnow()) __table_args__ = (UniqueConstraint('name', 'parent'),) def __init__(self, name, parent): self.name = name self.parent = parent def __repr__

Preserve existing tables in database when running Flask-Migrate

拥有回忆 提交于 2019-12-03 15:35:24
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 = Migrate(app, db) manager = Manager(app) manager.add_command('db', MigrateCommand) class User(db.Model

Relative importing python module from a subfolder from a different subfolder

。_饼干妹妹 提交于 2019-12-03 14:21:41
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" class imported |----configs/config.py <--- I want to import the class inside here. inside env.py: from

Can Alembic Autogenerate column alterations?

最后都变了- 提交于 2019-12-03 10:45:53
问题 I was able to use alembic --autogenerate for when adding / removing columns. However, when I wanted to modify for example a "url" column from 200 characters to 2000 characters, it doesn't detect the change. How can I make Alembic (using SQLAlchemy), detect changes and autogenerate scripts to my model's "sizes" of various columns and create "alter_column" commands for PostgreSQL ?? Edit: Why doesn't alembic automatically add: op.alter_column('mytable', 'url', type_=sa.String(2000), existing

Alembic autogenerates empty Flask-SQLAlchemy migrations

怎甘沉沦 提交于 2019-12-03 10:03:26
问题 I'm using Alembic to handle migrations for Flask. alembic revision --autogenerate should, in theory, autogenerate a migration based on changes in my database. However, Alembic is simply generating a blank migration with the above command. There's a question very similar to this one, where the issue was that the proper models weren't being imported. However, I have imported the models from my Flask app, as shown in env.py : ... # import settings from Flask alembic_config = config.get_section

Integrating Alembic with SQLAlchemy

南楼画角 提交于 2019-12-03 08:42:32
I'm looking at a way to integrate Alembic with SQLAlchemy. What I need is a way so that Alembic detects any changes I make in models.py automatically and updates it in the MySQL database when I run alembic revision -m "<message_here>" and alembic upgrade head . Here is what I have at the moment. This is my application directory structure. /myapplication models.py __init__.py app.py /migrations env.py script.py.mako /versions The models.py contains the following. from sqlalchemy import Column, Integer, String from sqlalchemy.ext.declarative import declarative_base Base = declarative_base()

How do you add migrate an existing database with alembic/flask-migrate if you did not start off with it?

て烟熏妆下的殇ゞ 提交于 2019-12-03 07:42:33
问题 This is the chain of events that has and is happening Day 0: I developed and deployed my app Day 1: I create the new database Day 3: I realized I wanted to add a new row to my existing table. I found flask-migrate and I want to use it to migrate my database. Currently I am at Day 3 There are plenty of documentations on how to get Flask-migrate running if you start from Day 0. You just call flask db init , flask db migrate and flask db upgrade . However, for my case, it's a bit different. I

Alembic: IntegrityError: “column contains null values” when adding non-nullable column

怎甘沉沦 提交于 2019-12-03 05:32:07
问题 I'm adding a column to an existing table. This new column is nullable=False . op.add_column('mytable', sa.Column('mycolumn', sa.String(), nullable=False)) When I run the migration, it complains: sqlalchemy.exc.IntegrityError: column "mycolumn" contains null values 回答1: It is because your existing data have no value on that new column, i.e. null . Thus causing said error. When adding a non-nullable column, you must decide what value to give to already-existing data Alright, existing data

Fetch table values using alembic and update to another table.

十年热恋 提交于 2019-12-03 05:08:35
I have oauth secret and oauth key in client table. Now I moving them to oauth credentials table which will be created during migration. Alembic produced following schema for upgrade. from myapp.models import Client, ClientCredential from alembic import op import sqlalchemy as sa def upgrade(): ### commands auto generated by Alembic - please adjust! ### op.create_table('client_credential', sa.Column('id', sa.Integer(), nullable=False), sa.Column('created_at', sa.DateTime(), nullable=False), sa.Column('updated_at', sa.DateTime(), nullable=False), sa.Column('client_id', sa.Integer(), nullable