skunk-tables icon indicating copy to clipboard operation
skunk-tables copied to clipboard

Add a notion of foreign keys

Open chuwy opened this issue 10 months ago • 0 comments

We can declare some members of a table class as "foreign keys", i.e. instead of an actual column in a table they're references to another relation-table. For example:

A persons table:

id first_name last_name
1  John       Smith  

A Person class:

case class Person(firstName: String, lastName: String, photos: List[Photo.Id])

Here, photos is a reference to persons_photos relation table:

person_id photo_id
1         1
1         2
1         3

So expressing this relation would look like:

Table
  .of[Person]
  .withName("persons")
  .withPrimary("id")
  .withForeign[Photo.Id]("photos", "id")
  .build

This way, whenever we do Person.table.get(personId) - it also queries all photos.

chuwy avatar Oct 19 '23 05:10 chuwy