最新消息:20190717 VPS服务器:Vultr新加坡,WordPress主题:大前端D8,统一介绍入口:关于

【记录】用Flask-Migrate去升级数据库修改字段

database crifan 659浏览 0评论

修改前:

修改后:

代码:

    createdAt = db.Column(db.DateTime, nullable=False, default = datetime.now)
    updatedAt = db.Column(db.DateTime, nullable=False, default = datetime.now, onupdate = datetime.now)
    def __repr__(self):
        return ‘<User:firstName=%r,lastName=%r,id=%s,phone=%s,email=%s,createdAt=%s,updatedAt=%s>’ % (self.firstName, self.lastName, self.id, self.phone, self.email, self.createdAt, self.updatedAt)

效果:

(RunningFast) ➜  staging python db_manager.py db migrate

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:63]:
redis_store=<flask_redis.FlaskRedis object at 0x7f0ab2587110>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=7121239c-580e-464e-8fe0-5a37333a6076, newUuid=user-7121239c-580e-464e-8fe0-5a37333a6076

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/RunningFast/staging/runningfast/views.py:44]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.autogenerate.compare] Detected added column ‘users.createdAt’
INFO  [alembic.autogenerate.compare] Detected added column ‘users.updatedAt’
INFO  [alembic.autogenerate.compare] Detected removed column ‘users.created_at’
INFO  [alembic.autogenerate.compare] Detected removed column ‘users.modified_at’
  Generating /root/RunningFast/staging/migrations/versions/df80d262d138_.py … done
(RunningFast) ➜  staging python db_manager.py db upgrade

<div–<——————————————————————————

DEBUG in __init__ [/root/RunningFast/staging/runningfast/__init__.py:63]:
redis_store=<flask_redis.FlaskRedis object at 0x7f2f8029a110>, db=<SQLAlchemy engine=’mysql://runningfast:Jiandao123@localhost/runningfast_dev’>, server_mode=staging, server_type=develop

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in models [/root/RunningFast/staging/runningfast/models.py:13]:
prefix=user-, generatedUuid4Str=9e65e8c9-1c9d-4a46-8c7a-2433b73ab309, newUuid=user-9e65e8c9-1c9d-4a46-8c7a-2433b73ab309

<div–<——————————————————————————

<div–<——————————————————————————

DEBUG in views [/root/RunningFast/staging/runningfast/views.py:44]:
API_VERSION=1.0, API_URL_PREFIX=/runningfast/api/v1.0, OPEN_API_URL_PREFIX=/runningfast/api/v1.0/open

<div–<——————————————————————————

INFO  [alembic.runtime.migration] Context impl MySQLImpl.
INFO  [alembic.runtime.migration] Will assume non-transactional DDL.
INFO  [alembic.runtime.migration] Running upgrade  -> df80d262d138, empty message

果不其然:

数据库,字段改名后,所有的数据都不见了

然后重新创建了新的数据:

datetime的默认的0值

【后记】

后来找到解决办法了,用op.alter_column去重命名已有列,即可保留已有数据:

【已解决】Flask-Migrate升级MySQL字段时能否重命名而非删除后新建

转载请注明:在路上 » 【记录】用Flask-Migrate去升级数据库修改字段

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
58 queries in 0.114 seconds, using 18.93MB memory