How can I handle a > 22 column table with Slick using nested tuples or HLists?

眉间皱痕 提交于 2019-11-30 08:09:01

Here I write a post to give out the solution. Here is the link: https://lihaimei.wordpress.com/2016/03/30/slick-1-fix-more-than-22-columns-case/

I draw some graphs and use different colors to help you understand fast.

To summarize, I use additional case Class to package some columns to one, which will not influence real physical columns. And then when we use projection to map to a custom type, we involve tuple back. This is a hack solution, but it is easily to fix Scala programming language's limit where the size of tuples should be less than 22.

The test code you linked to is outdated. If you don't use mappings for your tables, it's straight-forward: The type of * corresponds to the return type you get when querying the table, whether it is a single tuple, an HList, or nested tuples. Since Slick 2.1 this works for all operations. (In 2.0 it was not supported for the * projection, so you had to define an alternate projection and override create_*.) See here for an HList example.

If you want to map the * projection to a custom type, you also use the <> operator as for a single tuple but you don't get the convenience of the tupled and unapply methods that are automatically generated for case classes, so you have to write the two mapping functions (from the unmapped to the mapped type and back) manually as shown here. Note that Scala 2.11 does not improve this situation. While it allows case classes with more than 22 fields, there are no corresponding Function types for arities > 22, so you still can't use tupled and unapply.

As an alternative to writing these functions, you can define a lifted type corresponding to your mapped type as explained in the manual. This is especially useful when you have nested case classes (of <= 22 fields each) for your mapped type. You only have to define separate mappings for each case class and they will automatically compose when you use them in a * projection (or any other place in a projection or query).

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!