I'm trying to create a database to handle my tasks in the Play! Framework.
Here's what I have:
In build.sbt
:
libraryDependencies ++= Seq(
jdbc,
cache,
ws,
evolutions,
"com.typesafe.play" %% "anorm" % "2.5.0",
"org.scalatestplus.play" %% "scalatestplus-play" % "1.5.0-RC1" % Test
)
In conf/evolutions/default/1.sql
:
default.driver = org.h2.Driver
default.url = "jdbc:h2:mem:play"
I'm importing anorm in my app/models/Task.scala
and defining a Task as:
import anorm._
import anorm.SqlParser._
case class Task(id: Int, name: String, description: String, group: String)
And this is in my conf/evolutions/default/1.sql
:
#Tasks Schema
# ---- !Ups
CREATE SEQUENCE task_id_seq;
CREATE TABLE task (
id integer NOT NULL DEFAULT nextval('task_id_seq'),
label varchar(255),
description varchar(255),
group varchar(255)
);
# ---- !Downs
DROP TABLE task;
DROP SEQUENCE task_id_seq;
I feel like I'm doing everything correct, and when I refresh my server at http://localhost:9000 these are the errors I receive:
https://i.sstatic.net/cAIKl.png
I'm told this is fine, but when I click "Apply this script" I receive this error:
https://i.sstatic.net/kMPN7.png
How can I fix this? What am I doing wrong?
Here's more detail about the error:
[error] p.a.d.e.DefaultEvolutionsApi - Table "TASK" not found; SQL statement:
DROP TABLE task [42102-191] [ERROR:42102, SQLSTATE:42S02]
[error] application -
! @6pm2o74ag - Internal server error, for (GET) [/@evolutions/apply/default?redirect=http%3A%2F%2Flocalhost%3A9000%2F] ->
play.api.db.evolutions.InconsistentDatabase: Database 'default' is in an inconsistent state![An evolution has not been applied properly. Please check the problem and resolve it manually before marking it as resolved.]
at play.api.db.evolutions.DatabaseEvolutions.checkEvolutionsState(EvolutionsApi.scala:276)
at play.api.db.evolutions.DatabaseEvolutions.databaseEvolutions(EvolutionsApi.scala:126)
at play.api.db.evolutions.DatabaseEvolutions.scripts(EvolutionsApi.scala:102)
at play.api.db.evolutions.DatabaseEvolutions.scripts(EvolutionsApi.scala:117)
at play.api.db.evolutions.DefaultEvolutionsApi.scripts(EvolutionsApi.scala:82)
at play.api.db.evolutions.EvolutionsWebCommands.handleWebCommand(ApplicationEvolutions.scala:360)
at play.core.DefaultWebCommands$$anonfun$handleWebCommand$1.apply(WebCommands.scala:40)
at play.core.DefaultWebCommands$$anonfun$handleWebCommand$1.apply(WebCommands.scala:40)
at scala.collection.immutable.Stream.flatMap(Stream.scala:489)
at play.core.DefaultWebCommands.handleWebCommand(WebCommands.scala:40)
EDIT:
Okay so I added the lines
db.default.enabled = true
applyEvolutions.db=true
applyEvolutions.default=true
applyDownEvolutions.default=true
in my /application.conf/
file to see if that would make a difference, and now when I try to Apply this Script!
I receive an different error. Now I'm getting an error that says my syntax in my script is incorrect. Can anyone find it?
group
is a reserved keyword, you have to put it between quotes if you really want to use it:
CREATE TABLE task (
id integer NOT NULL DEFAULT nextval('task_id_seq'),
label varchar(255),
description varchar(255),
"group" varchar(255)
)
When you get the "inconsistent state" error you can also go directly to the database and check the play_evolutions
table, it can be helpful to see which errors occurred.
Also, I recommend that you test your evolutions in the database before running them in play. First run the ups and then the downs. It is easier to detect syntax errors this way.