You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Scala Slick + slick-pg v0.21.1定义List[Int]列编译失败求助

解决Slick-pg v0.21.1中List[Int]列编译失败的问题

问题原因

编译报错could not find implicit value for parameter tt: slick.ast.TypedType[List[Int]],核心是缺少PostgreSQL数组类型(对应Scala的List[Int])的隐式类型映射。Slick本身不支持PostgreSQL数组,需要通过slick-pg的数组扩展来提供对应的隐式实例。

解决步骤

1. 修正自定义Profile的配置

确保你的PromoPostgresProfile正确混入slick-pg的数组扩展支持:

import com.github.tminglei.slickpg._

// 自定义Profile混入PgArraySupport以支持数组类型
trait PromoPostgresProfile extends ExPostgresProfile with PgArraySupport {
  // 在API中混入ArrayImplicits,提供数组类型的隐式转换
  override val api = new API with ArrayImplicits {}
}

object PromoPostgresProfile extends PromoPostgresProfile

这个配置会自动为List[Int](对应PostgreSQL的int[]类型)生成所需的TypedType隐式实例。

2. 修正代码中的列名笔误

你的代码里numbers列错误地使用了"id"作为列名,这会导致后续数据库操作冲突,需要修正:

import com.nbcuas.promo.common.db.PromoPostgresProfile.api._

class MyTable(tag: Tag) extends Table[MyObject](tag, "my_table") {
  def id: Rep[Int] = column[Int]("id")
  // 修正列名为"numbers",对应数据库中的int[]类型字段
  def numbers: Rep[List[Int]] = column[List[Int]]("numbers")
}

3. 验证依赖完整性

确保build.sbt中已正确引入slick-pg依赖:

libraryDependencies += "com.github.tminglei" %% "slick-pg" % "0.21.1"

完成以上步骤后,编译应该能正常通过,slick-pg会自动处理List[Int]与PostgreSQL数组类型的映射。

内容的提问来源于stack exchange,提问作者CJC

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.19 18:32:49