将Play!框架的数据库从MySQL转换为PostgreSQL

4

我正在使用plaframework 2.2.1,我之前创建了一个MySQL项目,但现在我想将我的项目转移到PostgreSQL,但是在重新创建数据库演变时出现了一些错误。

我的旧的MySQL演变(1.sql)是可以正常工作的:

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

# --- !Ups

create table product (
  id                        bigint auto_increment not null,
  name                      varchar(255),
  price                     float,
  constraint pk_product primary key (id))
;

create table shop (
  id                        bigint auto_increment not null,
  name                      varchar(255),
  address_line1             varchar(255),
  address_line2             varchar(255),
  address_line3             varchar(255),
  city                      varchar(255),
  town                      varchar(255),
  phone_number              varchar(255),
  owner_email               varchar(255),
  constraint pk_shop primary key (id))
;

create table user (
  email                     varchar(255) not null,
  password                  varchar(255),
  first_name                varchar(255),
  last_name                 varchar(255),
  constraint pk_user primary key (email))
;


create table product_shop (
  product_id                     bigint not null,
  shop_id                        bigint not null,
  constraint pk_product_shop primary key (product_id, shop_id))
;
alter table shop add constraint fk_shop_owner_1 foreign key (owner_email) references user (email) on delete restrict on update restrict;
create index ix_shop_owner_1 on shop (owner_email);



alter table product_shop add constraint fk_product_shop_product_01 foreign key (product_id) references product (id) on delete restrict on update restrict;

alter table product_shop add constraint fk_product_shop_shop_02 foreign key (shop_id) references shop (id) on delete restrict on update restrict;

# --- !Downs

SET FOREIGN_KEY_CHECKS=0;

drop table product;

drop table product_shop;

drop table shop;

drop table user;

SET FOREIGN_KEY_CHECKS=1;

然后我已经删除了1.sql,并重新创建了我的进化(1.sql)用于postgresql,如下所示。
 # --- !Ups

create table member (
  email                     varchar(255) PRIMARY KEY,
  password                  varchar(255),
  first_name                varchar(255),
  last_name                 varchar(255)
  )
;

create table product (
  id                        bigserial PRIMARY KEY,
  name                      varchar(255),
  price                     real
  )
;

create table shop (
  id                        bigserial PRIMARY KEY,
  name                      varchar(255),
  address_line1             varchar(255),
  address_line2             varchar(255),
  address_line3             varchar(255),
  city                      varchar(255),
  town                      varchar(255),
  phone_number              varchar(255),
  email                     varchar(255) REFERENCES member
  )
;


create table product_shop (
  product_id                     bigint REFERENCES product ON DELETE RESTRICT,
  shop_id                        bigint REFERENCES shop ON DELETE CASCADE,
  PRIMARY KEY (product_id, shop_id)
  )
;

这两个 SQL 有什么区别吗?

我需要添加些什么来使得我的新的 1.sql 函数与旧的 1.sql 在我的 MySQL 进化中相等吗?我的新进化创建了我的数据库,但是当我尝试在我的商店表中插入值时,它显示相同的页面,并且与 MySQL 工作方式不同,即没有加载下一页。在产品表中插入时会显示这个。

[PersistenceException: Error getting sequence nextval]
In C:\Users\Myproject\app\models\Product.java at line 36.
33
34    public static Product create(String name,float price) {
35        Product product = new Product(name, price);
36        product.save();
37        product.saveManyToManyAssociations("shops");
38        return product;
39    }
40    public static void delete(Long id) {
41        find.ref(id).delete();

我也无法在PgAdmin III中找到由2.sql创建的数据库?

当你说“一些错误”时,你到底是指什么?请提供精确的错误信息文本。 - Craig Ringer
我的进化创造了我的数据库,我可以通过浏览器中的项目向我的会员表插入值,但是当我输入网址以显示页面时,显示数据插入表单的页面时,它显示错误PersistanceException,表示关系"product"不存在。 - akku
请将错误信息复制并粘贴到您的问题中。使用“编辑”按钮。 - Craig Ringer
抱歉,我现在编辑了我的问题。 - akku
那是一个完全不同于你之前提到的错误。它是什么?我还建议你查看PostgreSQL 服务器错误日志,看看它是否有更多细节,因为这些错误缺少重要信息。 - Craig Ringer
我正在尝试将我的项目部署到Heroku,但在本地使用Heroku db.default.url设置时遇到了一些演化问题,导致出现PerrsistanceException关系未找到的错误。因此,我更改了我的db.default.url="jdbc:postgresql://localhost:5432/dbname",但仍然出现错误Error getting sequence nextval,所以我的主要目标是编写正确的演化,以便它可以在本地和Heroku上运行。 - akku
1个回答

0

确保数据库处于一致的状态。

假设您尚未从以前的MySQL数据库迁移数据,并且您正在开发模式下工作(而不是生产模式),因此您无需担心保留数据:

  • 将您的迁移文件重命名为1.sql。仅因为您在以前的数据库中执行了迁移,这并不意味着当您在一个全新的数据库中执行它时,它是第二个进化:对于新数据库来说,它仍然是第一个。
  • 像这样声明您的主键列:id bigserial primary key并删除constraint
  • 确保您在PostgreSQL中有一个空数据库。删除数据库并重新创建它(dropdb, createdb)。
  • 运行您的数据库迁移,并查看输出以确保已执行迁移。请参见管理数据库进化
  • 使用PgAdmin或类似工具,如Eclipse的Toad扩展程序,验证数据库结构是否正确创建。

或者,您可能会发现Flyway提供了更全面的数据库迁移方法。Play Framework有一个插件

为了避免异常Error getting sequence nextval,请正确注释实体类定义,如下所示:

@Id
@SequenceGenerator(name="product_gen", sequenceName="product_id_seq", allocationSize=1)
@GeneratedValue(strategy=GenerationType.SEQUENCE, generator="product_gen")
@Column(name="id")
public Long getId() { return id; }

检查数据库以确保sequenceName是由PostgreSQL创建的序列的名称。
更多信息请参见:

我重新创建了我的进化并将其命名为1.sql,但浏览器显示“应用此脚本”按钮与我之前删除的1.sql的下部分重叠,现在我感到困惑。 - akku
这听起来像是您正在对已经存在一些表的数据库进行申请。删除该数据库,重新创建并重试。 - Fernando Correia
如果演变显示先前版本的内容,则存在一些先前尝试的残留物。看起来你正在开发针对在Heroku上运行的远程PostgreSQL数据库。所以我建议你删除该数据库并重新创建它。 - Fernando Correia
我已经重新创建了我的1.sql(如上所述),删除了数据库,但仍然出现错误[PersistenceException: Error getting sequence nextval]。在我的演化中是否有遗漏?因为我能够在成员表中插入值,但无法在产品表中插入值。 - akku
@akku 目前我相信迁移已经成功了,但实体类的注释似乎需要修复。我已经补充了有关正确注释的信息。 - Fernando Correia
显示剩余3条评论

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接