4

I am using plaframework 2.2.1, I had made a project MySQL but now i want to shift my project to PostgreSQL but having some errors recreating the DB evolution.

My old evolution(1.sql) for mysql which worked fine is:

# --- 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;

Then i have deleted 1.sql and recreated my evolution(1.sql) for postgresql given below

 # --- !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)
  )
;

Is there any difference between the two sql?

Do i need to add something to make my new 1.sql function equal to that of my old 1.sql in my mysql evolution? My new evolution created my database but when i am trying to insert values in my shop table it shows same page and its not working same as it worked with mysql,means not loading the next page. When inserting in product table it shows this.

[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();

I also cant find my database created by 2.sql in PgAdmin III?

akku
  • 469
  • 5
  • 17
  • When you say "some error", what EXACTLY do you mean? Exact error message text please. – Craig Ringer Apr 02 '14 at 00:44
  • i mean my evolution creates my database,i can insert values in my member table through my project in browser but when i am entering url to display the page which shows me the form to insert data in product details it shows error PersistanceException reloation product does not exist – akku Apr 02 '14 at 04:47
  • Please *copy and paste the error* into your question. Use the "edit" button. – Craig Ringer Apr 02 '14 at 08:09
  • sorry,edited my question now – akku Apr 02 '14 at 09:36
  • Well, that's a totally different error to what you mentioned before. Which is it? I also suggest you look at the PostgreSQL *server* error logs, see if it has any more detail, as those errors are missing important information. – Craig Ringer Apr 02 '14 at 10:10
  • i am trying to deploy my project to heroku but having some evolution problem running locally with heroku db.default.url settings ,giving the error PerrsistanceException relation not found due to this i changed my db.default.url="jdbc:postgresql://localhost:5432/dbname" but then also i am getting the error Error getting sequence nextval,so my main aim is to write a correct evolution so that it can run locally and on heroku – akku Apr 02 '14 at 17:57

1 Answers1

0

Make sure the database is in a consistent state.

Assuming you have not migrated the data from the previous MySQL database and that you're working in development mode (not in production mode), so you don't have to worry about preserving data:

  • Rename your migration back to 1.sql. Just because you executed the migration in a previous database, it doesn't mean that when you're going to execute it in a completely new database it's a second evolution: for the new database, it's still the first one.
  • Declare your primary key columns like this: id bigserial primary key and remove the constraint.
  • Make sure you have an empty database in PostgreSQL. Drop the database and recreate it (dropdb, createdb).
  • Run your database migration and watch the output to make sure the migration was executed. See Managing database evolutions.
  • Use PgAdmin or a similar tool such as Toad Extension for Eclipse to verify that the database structure was correctly created.

Alternatively, you may find out that Flyway provides a more comprehensive approach to database migrations. There is a plugin for Play Framework.

To avoid the exception Error getting sequence nextval, properly annotate the entity class definitions like this:

@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; }

Inspect the database to make sure that sequenceName is the name of the sequence created by PostgreSQL.

For more information see:

Community
  • 1
  • 1
Fernando Correia
  • 21,803
  • 13
  • 83
  • 116
  • i have recreated my evolution and named it 1.sql,but the browser shows apply this script button with down part of my previous 1.sql that i had deleted,i am confused now – akku Apr 02 '14 at 17:41
  • This sounds like you're applying over a database which already has some tables. Drop the database, recreate it and try again. – Fernando Correia Apr 02 '14 at 18:15
  • i have droped the database but it still showing error from my previous 1.sql that i have deleted – akku Apr 03 '14 at 05:10
  • my main aim to shift to postgresql is that i want to deploy my project on heroku so i am using heroku db.default.url configuration to run my project locally `db.default.driver=org.postgresql.Driver db.default.url="jdbc:postgresql://ec2-107-22-163-140.compute-1.amazonaws.com:5432/d6s749rg71m651?user=****&password=****&ssl=true&ssl=true&sslfactory=org.postgresql.ssl.NonValidatingFactory" #db.default.user="postgres" #db.default.password="postgres"` – akku Apr 03 '14 at 05:42
  • If the evolution is showing content from the previous version, there is some leftover from previous attempts. It seems that you're developing against the remote PostgreSQL database running on Heroku. So I suggest you drop that database and recreate it. – Fernando Correia Apr 03 '14 at 11:10
  • i have recreated my 1.sql(pasted above),dropped the database but it still giving error `[PersistenceException: Error getting sequence nextval]` am i missing something in my evolution? because i am able to insert values in member table but not in product – akku Apr 04 '14 at 17:40
  • @akku At this point I believe the migration was successful, but the entity class' annotations seem to be needing to be fixed. I've complemented my answer with information about the proper annotations. – Fernando Correia Apr 04 '14 at 23:53
  • thanks @Fernando the product table problem is solved but when i am submitting the shop form it loads the same page unlike in case of mysql ,created a new question for that hope u can help http://stackoverflow.com/questions/22879684/if-condition-always-routing-towards-haserror-condition-while-submitting-a-form – akku Apr 05 '14 at 17:03