0

I have a database schema defined in mySQL already and I want to work on the play-2 with ActiveRecord application on top of it.

However, when I start up the project, it gives me error:

com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'user' already exists
Caused by: com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: Table 'user' already exists
and it is triggered by 
 org.squeryl.Schema.create(Schema.scala:181)
 models.Tables$.initialize(Tables.scala:7)

This is how it looks in my Tables.scala

object Tables extends ActiveRecordTables with PlaySupport {
    val users = table[User]
}

and my User.scala is:

case class User( 
    override val id: Long,  
    @Length(max=50) login: String
) extends ActiveRecord {
    lazy val role = belongsTo[Role]
}
object User extends ActiveRecordCompanion[User]

I tried to skip this in my global.scala

override def onStart(app: Application) {
    //Tables.initialize
} 

However, it still give me the same error

Is that anyway I can bypass the create table part?

Many thanks!

jruizaranguren
  • 12,679
  • 7
  • 55
  • 73
Joyfulvillage
  • 547
  • 1
  • 4
  • 13

3 Answers3

0

Do you already have a user table in your database with a different schema?

Since you are using the PlaySupport trait, I am guessing you are using the Scala ActiveRecord Play2.1 Plugin. As noted in the wiki of that project, add the following settings in conf/play.plugins

9999:com.github.aselab.activerecord.ActiveRecordPlugin

The ActiveRecordPlugin class initializes the tables on startup. So you shouldn't have to do that from your Global onStart

mantithetical
  • 1,755
  • 2
  • 16
  • 21
  • I check out the source code of activerecord plugin and see the onstart and onclose for initialization and cleanup. The schema in database is a bit different from the one I declare in User.scala: I have a enum in the user table which ActiveRecord is not supporting so I have a case class instead. Also, I have an password field that I indeed put in object: extends ActiveRecord { @Transient @Length(max = 50) var password: Option[String] = _ lazy val role = belongsTo[Role] } – Joyfulvillage Jun 13 '13 at 14:52
0

Maybe you use something like MyDatabaseLibrary.create() yourself ?

Check it out. You may try to search the directory for occurences of .create

VasiliNovikov
  • 9,681
  • 4
  • 44
  • 62
0

I figure it out the issue by commenting out some of the table schema in Tables.scala. I have multiple tables declared in Table object and one of them is not existed in the database, for example:

object Tables extends ActiveRecordTables with PlaySupport {
  val users = Table[User]
  val role = Table[Role]
  val group = Table[Group] //not exist which cause the error!
}

If one of the table is not exist in the database, the framework will create ALL tables in the order that listed in the object.
I did try to arrange the non-existing record to the top and the framework would create the table and failed to run. However, if a situation as listed above, it would give error: table 'user' is already declared and NO table would be created in database.

Would raise as a bug to Activerecord and see if there's a solution.

Joyfulvillage
  • 547
  • 1
  • 4
  • 13
  • this is a really annoying behaviour - I copied the report and opened an issue at https://github.com/aselab/scala-activerecord/issues/72 – Makibo Jun 15 '15 at 02:21