I have a table like this:
object Addresses extends Table[AddressRow]("address") {
def id = column[Int]("id", O.PrimaryKey, O.AutoInc)
def street = column[String]("street")
def number = column[String]("number")
def zipcode = column[String]("zipcode")
def city = column[String]("city")
def country = column[String]("country")
def geoLocationId = column[Int]("geo_location_id", O.Nullable)
// Foreign keys.
def geoLocation = foreignKey("fk_geo_location", geoLocationId, GeoLocations)(_.id)
// Rest of my code.
...
}
where my case class is:
case class AddressRow(
id: Option[Int] = None,
street: String,
number: String,
zipcode: String,
city: String,
country: String,
geoLocationId: Option[Int])
As you notice geoLocation is an optional foreign key....
I can't find any way to describe this "Optional" in my foreign key definition.
I've tried like:
def geoLocation = foreignKey("fk_geo_location", geoLocationId.asColumnOf[Option[Int]], GeoLocations)(_.id)
but I receive:
Caused by: scala.slick.SlickException: Cannot use column Apply Function Cast in foreign key constraint (only named columns are allowed)
Does anybody has a suggestion?