Search code examples
scalaplayframeworkslick

Why doesn't MySQLDriver have a Nullable Column Option?


I have this line of code in my slick table definition

def firstName = column[Option[String]]("first_name", O.Nullable)

but I get this error at compile time

[error] /Users/roy/adivinate/survey2/app/model/Respondent.scala:36:
 value Nullable is not a member of slick.driver.MySQLDriver.ColumnOptions

I am confused because this is exactly the syntax that I have seen in other code although I'm not sure if they were using MySQLDriver. What am I suppose to do for null columns?


Solution

  • O.Nullable is deprecated. Declaring the field Option[T] will do the job.

    Declare your field in the model class as Option[T] instead of T to make that corresponding column Nullable

    Lets understand this with an example

    case class Foo(name: String, rating: Option[Int])
    
    class Foos(tag: Tag) extends Table[Foo](tag, "foos") {
      def name = column[String]("name") //name is not null
      def rating = column[Option[Int]]("rating") //rating is nullable
      def * = (name, rating) <> (Foo.tupled, Foo.unapply)
    }
    

    If you want to make something nullable just declare it as Option field and thats it slick will understand and generate sql with that particular field as nullable.

    The above design is seamless and sound with Scala Option design. The meaning is option in Scala is directly converted to the Nullable in sql.

    In older versions of Slick

    You had to tell that particular column is not null by explicitly passing O.NotNull in the column declaration, but its not required in new version of slick