修改前:
修改后:
代码:
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去升级数据库修改字段