使用 Scala Slick 创建组合主键

2024-05-24

我正在尝试使用两列作为 Scala Slick 表的主键。这是我的表的定义方式:

class NbaPlayerBoxScoreTable(tag : Tag) extends Table[NbaPlayerBoxScore](tag, "player_box_scores") {
  import com.suredbits.core.db.ColumnMappers._
  private val gameTable : TableQuery[NbaGameTable] = TableQuery[NbaGameTable]
  private val playerTable  : TableQuery[NbaPlayerTable] = TableQuery[NbaPlayerTable]
  def playerId = column[Long]("player_id", O.PrimaryKey)
  def gameId = column[Long]("game_id", O.PrimaryKey)
  def lastUpdated = column[DateTime]("last_updated")
  def min = column[String]("min")
  def fgm = column[Int]("fgm")
  def fga = column[Int]("fga")
  def tpm = column[Int]("tpm")
  def tpa = column[Int]("tpa")
  def ftm = column[Int]("ftm")
  def fta = column[Int]("fta")
  def plusminus = column[Int]("plusminus")
  def off = column[Int]("off")
  def deff = column[Int]("def")
  def tot = column[Int]("tot")
  def ast = column[Int]("ast")
  def pf = column[Int]("pf")
  def st = column[Int]("st")
  def to = column[Int]("to")
  def bs = column[Int]("bs")
  def ba = column[Int]("ba")
  def pts = column[Int]("pts")

  def game = foreignKey("game_fk",gameId, gameTable)(_.gameId)
  def player = foreignKey("player_id",playerId, playerTable)(_.playerId)

  def * = (playerId, gameId, lastUpdated, min,
    fgm,fga,tpm, tpa,ftm,fta,plusminus,off,deff,tot,ast,pf,st,to,bs,ba,pts) <> (NbaPlayerBoxScore.tupled, NbaPlayerBoxScore.unapply _)

}

你可以看到我想要的组合playerId and gameId成为我的作文主键。当我尝试创建表时出现以下错误

[error] (run-main-0) org.postgresql.util.PSQLException: ERROR: multiple primary keys for table "player_box_scores" are not allowed
[error]   Position: 101
org.postgresql.util.PSQLException: ERROR: multiple primary keys for table "player_box_scores" are not allowed
  Position: 101
    at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2157)
    at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:1886)
    at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:255)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:555)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.executeWithFlags(AbstractJdbc2Statement.java:417)
    at org.postgresql.jdbc2.AbstractJdbc2Statement.execute(AbstractJdbc2Statement.java:410)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
    at org.apache.commons.dbcp.DelegatingPreparedStatement.execute(DelegatingPreparedStatement.java:172)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$1.apply(JdbcInvokerComponent.scala:50)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$1.apply(JdbcInvokerComponent.scala:50)
    at scala.slick.jdbc.JdbcBackend$SessionDef$class.withPreparedStatement(JdbcBackend.scala:191)
    at scala.slick.jdbc.JdbcBackend$BaseSession.withPreparedStatement(JdbcBackend.scala:389)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1$$anonfun$apply$mcV$sp$1.apply(JdbcInvokerComponent.scala:50)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1$$anonfun$apply$mcV$sp$1.apply(JdbcInvokerComponent.scala:49)
    at scala.collection.Iterator$class.foreach(Iterator.scala:743)
    at scala.collection.Iterator$JoinIterator.foreach(Iterator.scala:191)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1.apply$mcV$sp(JdbcInvokerComponent.scala:49)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1.apply(JdbcInvokerComponent.scala:49)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker$$anonfun$create$1.apply(JdbcInvokerComponent.scala:49)
    at scala.slick.jdbc.JdbcBackend$BaseSession.withTransaction(JdbcBackend.scala:426)
    at scala.slick.driver.JdbcInvokerComponent$DDLInvoker.create(JdbcInvokerComponent.scala:48)
    at com.suredbits.core.db.DbManagement$$anonfun$createTable$1.apply(DbManagement.scala:33)
    at com.suredbits.core.db.DbManagement$$anonfun$createTable$1.apply(DbManagement.scala:27)
    at scala.slick.backend.DatabaseComponent$DatabaseDef$class.withSession(DatabaseComponent.scala:34)
    at scala.slick.jdbc.JdbcBackend$DatabaseFactoryDef$$anon$3.withSession(JdbcBackend.scala:46)
    at com.suredbits.core.db.DbManagement$class.createTable(DbManagement.scala:27)
    at io.extrapoint.nbaapi.Main$.createTable(Main.scala:14)
    at io.extrapoint.nbaapi.db.NbaApiDbManagement$class.createNbaPlayerBoxScoreTable(NbaApiDbManagement.scala:39)
    at io.extrapoint.nbaapi.Main$.createNbaPlayerBoxScoreTable(Main.scala:14)
    at io.extrapoint.nbaapi.Main$.createBoxScoresForFinishedGames(Main.scala:64)
    at io.extrapoint.nbaapi.Main$.delayedEndpoint$io$extrapoint$nbaapi$Main$1(Main.scala:85)
    at io.extrapoint.nbaapi.Main$delayedInit$body.apply(Main.scala:14)
    at scala.Function0$class.apply$mcV$sp(Function0.scala:40)
    at scala.runtime.AbstractFunction0.apply$mcV$sp(AbstractFunction0.scala:12)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.App$$anonfun$main$1.apply(App.scala:76)
    at scala.collection.immutable.List.foreach(List.scala:381)
    at scala.collection.generic.TraversableForwarder$class.foreach(TraversableForwarder.scala:35)
    at scala.App$class.main(App.scala:76)
    at io.extrapoint.nbaapi.Main$.main(Main.scala:14)
    at io.extrapoint.nbaapi.Main.main(Main.scala)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:497)

使用 Scala slick 2.1.0 可以吗?


也许你可以尝试使用首要的关键 http://slick.lightbend.com/doc/3.2.0/schemas.html:

class A(tag: Tag) extends Table[(Int, Int)](tag, "a") {
  def k1 = column[Int]("k1")
  def k2 = column[Int]("k2")
  def * = (k1, k2)
  def pk = primaryKey("pk_a", (k1, k2))
  // compiles to SQL:
  //   alter table "a" add constraint "pk_a" primary key("k1","k2")
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

使用 Scala Slick 创建组合主键 的相关文章

随机推荐