I'm using PostgreSQL as my database and evolutions to create my table. Here's a sample table:
CREATE TABLE Workout (
id SERIAL PRIMARY KEY,
title varchar(255) NOT NULL,
description text NOT NULL,
distance float8 NOT NULL,
duration float8 NOT NULL,
postedAt timestamp NOT NULL,
athleteId bigint REFERENCES Athlete
);
The SERIAL keyword causes postgresql to create an index that's defined as follows:
-- Sequence: workout_id_seq
-- DROP SEQUENCE workout_id_seq;
CREATE SEQUENCE workout_id_seq
INCREMENT 1
MINVALUE 1
MAXVALUE 9223372036854775807
START 1
CACHE 1;
ALTER TABLE workout_id_seq
OWNER TO postgres;
When I start my app, I load a few workouts in a BootStrap.scala class and they have ids 1, 2 and 3, respectively.
However, when I try to add a new workout using the following code:
def postWorkout(id: Option[Long]) = {
val workout = params.get("workout", classOf[Workout])
Validation.valid("workout", workout)
if (Validation.hasErrors) {
renderArgs.put("template", "Profile/edit")
edit(id);
} else {
id match {
case Some(id) => {
Workout.update(workout)
}
case None => {
workout.postedAt = new java.util.Date
workout.athleteId = 1
Workout.create(workout)
flash += "success" -> ("Nice workout!")
}
}
Action(index())
}
}
It fails with the following error:
play.exceptions.JavaExecutionException: ERROR: duplicate key value violates unique constraint "workout_pkey"
Detail: Key (id)=(1) already exists.
at play.mvc.ActionInvoker.invoke(ActionInvoker.java:229)
at Invocation.HTTP Request(Play!)
Caused by: org.postgresql.util.PSQLException: ERROR: duplicate key value violates unique constraint "workout_pkey"
Detail: Key (id)=(1) already exists.
at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2102)
at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1835)
at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:257)
at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:500)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:388)
at org.postgresql.jdbc2.AbstractJdbc2Statement.executeUpdate(AbstractJdbc2Statement.java:334)
at play.db.anorm.Sql$class.execute1(Anorm.scala:918)
at play.db.anorm.SimpleSql.execute1(Anorm.scala:836)
at play.db.anorm.M$class.create(Anorm.scala:407)
at play.db.anorm.Convention$Magic.create(Anorm.scala:277)
at controllers.Profile$.postWorkout(Profile.scala:64)
Does anyone know why the sequence isn't reset when BootStrap.scala loads my initial-data.yml?
Thanks,
Matt