4

我正在使用plaframework 2.2.1,我做了一個項目MySQL,但現在我想將項目轉移到PostgreSQL,但有一些錯誤重新創建數據庫的演變。轉換播放!從MySQL到PostgreSQL的框架演化

我的老演進(1.SQL),用於其工作的罰款是mysql:

# --- 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)下面

# --- !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) 
) 
; 
給出的PostgreSQL

這兩個sql有什麼區別嗎?

我需要添加一些東西,以使我的新的1.sql功能與我的mysql進化中的舊版1.sql相同嗎?我的新進化創建了我的數據庫,但是當我試圖在我的商店表中插入值時,它顯示相同的頁面,並且它與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(); 

我也無法找到由2.SQL中的pgAdmin III創建我的數據庫?

+0

當你說「有些錯誤」時,你說的是什麼意思?請準確填寫錯誤消息文本。 –

+0

我的意思是我的進化創建了我的數據庫,我可以通過瀏覽器中的項目在我的成員表中插入值,但是當我輸入url來顯示向我顯示在產品詳細信息中插入數據的表單時,它顯示錯誤PersistanceException reloation product does不存在 – akku

+0

請*複製錯誤*並將其粘貼到您的問題中。使用「編輯」按鈕。 –

回答

0

確保數據庫處於一致狀態。

假設你沒有從以前的MySQL數據庫遷移的數據和你在開發模式(而不是在生產模式)工作,所以你不必擔心保存數據:

  • 將您的遷移重命名爲1.sql。僅僅因爲你在先前的數據庫中執行了遷移,並不意味着當你要在一個全新的數據庫中執行它時,這是第二次進化:對於新的數據庫,它仍然是第一個。
  • 聲明您的主鍵列如下:id bigserial primary key並刪除constraint
  • 請確保您在PostgreSQL中有一個空的數據庫。刪除數據庫並重新創建它(dropdbcreatedb)。
  • 運行數據庫遷移並觀察輸出以確保遷移已執行。見Managing database evolutions
  • 使用PgAdmin或類似工具(如Toad Extension for Eclipse)驗證數據庫結構是否已正確創建。

或者,您可能會發現Flyway提供了一種更全面的數據庫遷移方法。 Play Framework有plugin

爲避免該異常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所創建的序列的名稱。

欲瞭解更多信息,請參閱:

+0

我重新創建了我的進化,並命名爲1 .sql,但瀏覽器顯示應用此腳本按鈕與我已刪除的我以前的1.sql的下半部分,我現在很困惑 – akku

+0

這聽起來像你正在申請已經有一些表的數據庫。刪除數據庫,重新創建並重試。 –

+0

我已經下降了數據庫,但它仍然顯示我以前的1.sql錯誤,我已經刪除 – akku