1

I use a postgresql9.4+flask+sqlalchemy in my current project. For making database migrations I decided to use sqlalchemy-migrate. But I've met the problem that this tool does not represent default value for the field correctly. To simplify my question I isolated the problem. My project looks like this:

flask_sqlalchemy_migration
├── app
│   └── __init__.py
├── config.py
├── db_create.py
└── db_migrate.py

./app/__init__.py

from flask import Flask
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)
app.config.from_object('config')
db = SQLAlchemy(app)


class Base(db.Model):
    # __abstract__ = True
    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime, default=db.func.current_timestamp())

./config.py

import os
basedir = os.path.abspath(os.path.dirname(__file__))

# database settings
DBMS = 'postgresql'
adapter = 'psycopg2'
user = 'flask_sqlalchemy_migration_db_user'
host = 'localhost'
port = '5432'
database = 'flask_sqlalchemy_migration_db'

SQLALCHEMY_DATABASE_URI = DBMS + '+' + adapter + '://' + user + '@' + host + ':' + port + '/' + database
SQLALCHEMY_MIGRATE_REPO = os.path.join(basedir, 'db_repository')
SQLALCHEMY_TRACK_MODIFICATIONS = True

./db_create.py

#!env/bin/python
from migrate.versioning import api
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
from app import db
import os.path

db.create_all()
if not os.path.exists(SQLALCHEMY_MIGRATE_REPO):
    api.create(SQLALCHEMY_MIGRATE_REPO, 'database repository')
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
else:
    api.version_control(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO,
                        api.version(SQLALCHEMY_MIGRATE_REPO))

./db_migrate.py

#!env/bin/python
import imp
from migrate.versioning import api
from app import db
from config import SQLALCHEMY_DATABASE_URI
from config import SQLALCHEMY_MIGRATE_REPO
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
migration = SQLALCHEMY_MIGRATE_REPO + ('/versions/%03d_migration.py' % (v+1))
tmp_module = imp.new_module('old_model')
old_model = api.create_model(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
exec(old_model, tmp_module.__dict__)
script = api.make_update_script_for_model(SQLALCHEMY_DATABASE_URI,
                                          SQLALCHEMY_MIGRATE_REPO,
                                          tmp_module.meta,
                                          db.metadata)
open(migration, "wt").write(script)
api.upgrade(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
v = api.db_version(SQLALCHEMY_DATABASE_URI, SQLALCHEMY_MIGRATE_REPO)
print('New migration saved as ' + migration)
print('Current database version: ' + str(v))

Up to this point everything's fine. I can run db_create.py and db_migrate.py, and it'll create initial database with base table and appropriate columns. The problem begin when I add "modified_at" field to the Base model:

class Base(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    created_at = db.Column(db.DateTime,      default=db.func.current_timestamp())
    + modified_at = db.Column(db.DateTime, default=db.func.current_timestamp(),
                        onupdate=db.func.current_timestamp())

Then, if I run ./db_migrate.py I'll get such error:

File "/Users/ruslans/workspace/python/flask_sqlalchemy_migration/db_repository/versions/002_migration.py", line 10
    Column('created_at', DateTime, default=ColumnDefault(<sqlalchemy.sql.functions.current_timestamp at 0x108e7b1d0; current_timestamp>)),
                                                         ^
SyntaxError: invalid syntax

Indeed, if I go to the ./db_repository/versions/002_migration.py I'll find there incorrect code:

from sqlalchemy import *
from migrate import *


from migrate.changeset import schema
pre_meta = MetaData()
post_meta = MetaData()
base = Table('base', post_meta,
    Column('id', Integer, primary_key=True, nullable=False),
    Column('created_at', DateTime, default=ColumnDefault(<sqlalchemy.sql.functions.current_timestamp at 0x108e7b1d0; current_timestamp>)),
    Column('modified_at', DateTime, onupdate=ColumnDefault(<sqlalchemy.sql.functions.current_timestamp at 0x108e7b518; current_timestamp>), default=ColumnDefault(<sqlalchemy.sql.functions.current_timestamp at 0x108e7b400; current_timestamp>)),
)


def upgrade(migrate_engine):
    # Upgrade operations go here. Don't create your own engine; bind
    # migrate_engine to your metadata
    pre_meta.bind = migrate_engine
    post_meta.bind = migrate_engine
    post_meta.tables['base'].columns['modified_at'].create()


def downgrade(migrate_engine):
    # Operations to reverse the above upgrade go here.
    pre_meta.bind = migrate_engine
    post_meta.bind = migrate_engine
    post_meta.tables['base'].columns['modified_at'].drop()

Please, if anyone knows why it happens and what I can do with it - let me know. I use

  • Flask==0.11.1
  • Flask-SQLAlchemy==2.1
  • psycopg2==2.6.2
  • SQLAlchemy==1.1.2
  • sqlalchemy-migrate==0.10.0
ruslash
  • 864
  • 1
  • 8
  • 12

1 Answers1

0

Just modify you version script (e.g. ./db_repository/versions/002_migration.py) manually and replace representations with usual python code.

So, replace sqlalchemy.sql.functions.current_timestamp with:

from app import db ... Column('modified_at', DateTime, onupdate=ColumnDefault(db.func.current_timestamp) ...