How to request Slick with an additional foreign key to return all records with and without relationships?

I have an optional foreign key defined for an event that relates to EventType. I want to query for all events, even those events that have the event type None (null). This is the foreign key defined for the event.

def eventTypeId = column[Option[Long]]("event_type_id")
def eventType = foreignKey("event_type", eventTypeId, EventTypes.eventTypes)(_.id)

My initial query is as follows, but it only returns records that have a foreign key, since a foreign key is optional. How?

(for {
   p <- events
   e <- p.eventType
} yield (p, e))

I want to see all events with AND without a foreign key.

+4
source share
1 answer

, , Slick . , Slick 2, Slick 3.2 : https://github.com/slick/slick/issues/179

. :

events.leftJoin(eventTypes).on(_.eventTypeId === _.id).
map { case (e, et) => (e, et.name.?) }

..., Seq[(Event, Option[String])] ( , name, a String).

+3

All Articles