Chris Stewart Chris Stewart - 2 months ago 22
Scala Question

Slick 3.1.1 not found type 'column'

I'm following the Scala Slick beginner guide trying to create a simple schema, and I can't seem to find the 'column' type when importing the stuff in the beginning of the documentation.

import slick.driver.H2Driver.api._
import scala.concurrent.ExecutionContext.Implicits.global
/**
* Created by chris on 9/7/16.
*/
class BlockHeaderTable(tag: Tag) extends Table[BlockHeader](tag,"block_headers") {

def version: column[UInt32]

def previousBlockHash: column[DoubleSha256Digest]

def merkleRootHash: column[DoubleSha256Digest]

def time: column[UInt32]

def nBits: column[UInt32]

def nonce: column[UInt32]


}


and here is the error I am getting:


chris@chris-870Z5E-880Z5E-680Z5E:~/dev/bitcoins-spv-node$ sbt compile
[info] Loading project definition from
/home/chris/dev/bitcoins-spv-node/project [info] Set current project
to bitcoins-spv-node (in build
file:/home/chris/dev/bitcoins-spv-node/) [info] Compiling 1 Scala
source to
/home/chris/dev/bitcoins-spv-node/target/scala-2.11/classes... [error]
/home/chris/dev/bitcoins-spv-node/src/main/scala/org/bitcoins/spvnode/models/BlockHeaderTable.scala:14:
not found: type column [error] def version: column[UInt32] [error]

^ [error]
/home/chris/dev/bitcoins-spv-node/src/main/scala/org/bitcoins/spvnode/models/BlockHeaderTable.scala:16:
not found: type column [error] def previousBlockHash:
column[DoubleSha256Digest] [error] ^ [error]
/home/chris/dev/bitcoins-spv-node/src/main/scala/org/bitcoins/spvnode/models/BlockHeaderTable.scala:18:
not found: type column [error] def merkleRootHash:
column[DoubleSha256Digest] [error] ^ [error]
/home/chris/dev/bitcoins-spv-node/src/main/scala/org/bitcoins/spvnode/models/BlockHeaderTable.scala:20:
not found: type column [error] def time: column[UInt32] [error]

^ [error]
/home/chris/dev/bitcoins-spv-node/src/main/scala/org/bitcoins/spvnode/models/BlockHeaderTable.scala:22:
not found: type column [error] def nBits: column[UInt32] [error]

^ [error]
/home/chris/dev/bitcoins-spv-node/src/main/scala/org/bitcoins/spvnode/models/BlockHeaderTable.scala:24:
not found: type column [error] def nonce: column[UInt32] [error]

^ [error] 6 errors found [error] (compile:compileIncremental)
Compilation failed

Answer

The type of the columns is not column but Rep. column is actually a function that tells slick which column to use:

class BlockHeaderTable(tag: Tag) extends Table[BlockHeader](tag,"block_headers")  {

  def version: Rep[UInt32] = column[UInt32]("version")

  def previousBlockHash: Rep[DoubleSha256Digest] = column[DoubleSha256Digest]("previous_block_hash")

  ...
}

Also I'm not sure what types you are using, but those are not supported out of the box by slick see here. You will need write custom type mappers. For example an UInt32 mapper:

implicit val UInt32Mapper = MappedColumnType.base[UInt32, Long](
  u => u.toLong,                  // convert UInt32 to Long here
  l => UInt32(l)                  // and Long to UInt32 here
)