gpt4 book ai didi

scala - 使用 Scala Slick 创建组合主键

转载 作者:行者123 更新时间:2023-12-04 05:02:45 25 4
gpt4 key购买 nike

我正在尝试使用两列作为 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的组合和 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 吗?

最佳答案

也许您可以尝试使用 primaryKey :

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")
}

关于scala - 使用 Scala Slick 创建组合主键,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33704436/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com