Chris Stewart
Chris Stewart

Reputation: 1679

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

Upvotes: 1

Views: 1387

Answers (2)

Roman
Roman

Reputation: 5699

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
)

Upvotes: 2

Nagarjuna Pamu
Nagarjuna Pamu

Reputation: 14825

Slick will not understand custom types other than standard JDBC types like Timestamp, Long, String, Char, Boolean etc. In order to work with the custom types you have to provide Slick Mapping of custom types to jdbc types.

Provide Slick Mapping for UInt32 and DoubleSha256Digest

For Example

DateTime is custom type which slick does not understand, but slick understands java.sql.Timestamp.We provide a slick mapping. So that slick can understand how to deal with DateTime

implicit def jodaTimeMapping: BaseColumnType[DateTime] = MappedColumnType.base[DateTime, Timestamp](
dateTime => new Timestamp(dateTime.getMillis),
timeStamp => new DateTime(timeStamp.getTime))

Complete Example

case class Foo(str: String) //Foo needs Slick Mapping for below code to compile

implicit def fooMapping: BaseColumnType[Foo] = MappedColumnType.base[Foo, String](
str => Foo(str),
foo => foo.str)


case class Person(name: String, foo: Foo)

class Persons(tag) extends Table[Person](tag, "persons") {
  def name = column[String]("name")
  def foo = column[Foo]("foo") //use Foo directly because Implicit mapping is available in scope
  def * = (name, foo) <> (Person.tupled,  Person.unapply)
}

Upvotes: 0

Related Questions