
CREATE TYPE file_status AS ENUM ('new','uploading','queued','processing','done','Failed'); 和相关的领域
CREATE table files ( ... status file_status NOT NulL,...);
使用Scala 2.10和Typesafe Slick 1.0.1,我已经创建了我的files表的映射,除了状态字段外,它的工作正常,这需要自定义的file_status类型,一个字符串.
def status = column[fileStatus]("status") 我一直在玩Slick的TypeMapper,但还是不知道如何让它工作:
sealed trait fileStatusimplicit val fileStatusMapper: TypeMapper[String] = base[fileStatus,String]( s => s.toString,f => f(fileStatus))
我收到错误:类型不匹配;发现:models.files.fileStatus.type必需:Int
为什么需要Int?是因为TypeMapper吗?我也试过了
...f => f.toString// type mismatch; found : String required: models.files.fileStatusf => f// type mismatch; found : String required: models.files.fileStatus
感谢您帮助我理解这种映射的任何指示.
引用文档( http://slick.typesafe.com/doc/1.0.1/lifted-embedding.html#user-defined-functions-and-types):// An algebraic data type for booleanssealed trait Boolcase object True extends Boolcase object False extends Bool// And a TypeMapper that maps it to Int values 1 and 0implicit val boolTypeMapper = MappedTypeMapper.base[Bool,Int]( { b => if(b == True) 1 else 0 },// map Bool to Int { i => if(i == 1) True else False } // map Int to Bool) 使其适应文件状态:
sealed trait fileStatuscase object New extends fileStatuscase object Uploading extends fileStatus...implicit val fileStatusTypeMapper = MappedTypeMapper.base[fileStatus,String]( { case New => "new" case Uploading => "uploading" ... },{ case "new" => New case "uploading" => Uploading ... }) 更新:
另一个不那么冗余但可能也不太清晰的版本:
sealed trait fileStatuscase object New extends fileStatuscase object Uploading extends fileStatus...val statusMap = Map( New -> "new",Uploading -> "uploading",...)implicit val fileStatusTypeMapper = MappedTypeMapper.base[fileStatus,String]( statusMap,statusMap.map(_.swap))总结
以上是内存溢出为你收集整理的postgresql – 如何在Scala中使用Typesafe Slick创建自定义列类型?全部内容,希望文章能够帮你解决postgresql – 如何在Scala中使用Typesafe Slick创建自定义列类型?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)