玩!框架-Evolutions的数据库问题 [英] Play! framework - database issue with Evolutions

查看:125
本文介绍了玩!框架-Evolutions的数据库问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Play! Framework 2.0,我陷入了涉及数据库的烦人问题.

I'm using Play! framework 2.0 and I'm stuck on an annoying issue involving the database.

假设我有一个User(扩展Model)类,该类具有很少的属性(first_namelast_nameemailpassword等).

Suppose I have a User (extends Model) class which has few attributes (first_name, last_name, email, password etc).

在某些时候我想添加一个新的属性,让我们说last_ip(这实际上并不重要).因此,我将该属性添加到User类,进行编译并运行.

At some point I want to add a new attribute, lets say last_ip (it doesn't really matter what it is). So, I add the attribute to the User class, compile and run.

问题是:我收到有关数据库更改的红色警报(很明显),它要求我按"APPLY CHANGES" (如果我没记错的话).很好,但是! 所有数据库记录都将被删除

The thing is: I get this red alert about database changes (obviously) which asks me to press "APPLY CHANGES" (if I remember correctly). That's fine BUT! all the database records are erased!

结论:我想要一个新字段,但是我不想丢失已经添加到数据库中的所有记录.这可能吗?

In conclusion: I want to a new field but I don't want to lose all the records I already added to the database. Is this possible?

推荐答案

首先,您需要通过删除conf/evolutions/default/1.sql的前2条注释行来禁用自动生成Evolution文件:

First you need to disable automatic generation of Evolution files by deleting the first 2 commented lines of the conf/evolutions/default/1.sql:

# --- Created by Ebean DDL
# To stop Ebean DDL generation, remove this comment and start using Evolutions

# --- !Ups
...

然后,您需要创建一个名为conf/evolutions/default/2.sql的第二个文件,其中包含您对数据库架构的更新,其中包含Ups和Downs部分:

Then, you need to create a second file, called conf/evolutions/default/2.sql containing your update on the database schema with an Ups and a Downs section:

# --- !Ups
ALTER TABLE USER ADD COLUMN last_ip varchar(30) DEFAULT NULL;

# --- !Downs

ALTER TABLE USER DELETE COLUMN last_ip;

这篇关于玩!框架-Evolutions的数据库问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆