3

我已经有了一个带有一个迁移脚本的简单项目:

# --- !Ups

create table user (
  name        varchar(255) not null primary key,
  password    varchar(255) not null
);

insert into user values ('demo', 'demo');
insert into user values ('kuki', 'pass');


# --- !Downs

drop table if exists user;

我正在使用的数据库是内存中的 H2:

db.default.driver=org.h2.Driver
db.default.url=jdbc:h2:mem:play

然后我显然想查询一些数据。当我使用 anorm 时,一切正常:

case class User(name: String, password: String)
object User {

  val simple = {
    get[String]("user.name") ~/
    get[String]("user.password") ^^ {
      case name~password => User(name, password)
    }
  }

  def findByName(name: String): Option[User] = {
    DB.withConnection { implicit connection =>
     SQL("select * from user where name = {name}").on(
        'name -> name
      ).as(User.simple ?)
    }
  }
}

当我尝试对 ScalaQuery 做同样的事情时很不幸:

object User extends Table[(String, String)]("user") {

  lazy val database = Database.forDataSource(DB.getDataSource())

  def name = column[String]("name", O PrimaryKey, O NotNull)
  def password = column[String]("password", O NotNull)
  def * = name ~ password

  def findByName(name: String) = database withSession {
    implicit db: Session =>
      (for (u <- this if u.name === name) yield u.name ~ u.password).list
  }
}

我总是遇到同样的错误:

[JdbcSQLException: Tablela "user" nie istnieje Table "user" not found; 
SQL statement: SELECT "t1"."name","t1"."password" FROM "user" "t1" WHERE ("t1"."name"='input_name') [42102-158]]



有什么我做错了吗?我想我严格遵循那里的指南:https

://github.com/playframework/Play20/wiki/ScalaDatabase --------- 编辑---- ------------------

看起来这是 Play 的演变和 ScalaQuery 之间的某种不兼容。当我使用以下方法创建表时:

database withSession {
  implicit db: Session =>
    User.ddl.create
    User.insert("demo", "demo")
}

一切似乎都很好。也许稍后我会创建一些简单的 MySQL 数据库并检查里面到底发生了什么。

--------------------- 编辑 2 ------------

所以我或多或少知道发生了什么(但我不知道为什么)。当我使用进化创建数据库结构时,表名和列名都用大写字母写下来。
而且由于我在Linux上,所以这很重要。如果我将代码中的表名和列名更改为大写,那么一切正常。

我只是好奇它是否是一个错误,或者是否有任何方法可以对迁移执行适当的案例?

4

1 回答 1

5

Most likely, the problem is that the Play! Framework quotes the identifier names (table names, column names) in the query, so that you need to quote the table name in the 'create table' statement as well:

create table "user" (
  "name"        varchar(255) not null primary key,
  "password"    varchar(255) not null
);
于 2012-01-04T06:20:00.110 回答