Exposed
Exposed copied to clipboard
Extra columns in bridging table
For many-to-many relations, the creation of bridging tables is often necessary. In some cases, these relations may require some extra data. To use the wiki example:
// create film
val film = transaction {
StarWarsFilm.new {
name = "The Last Jedi"
sequelId = 8
director = "Rian Johnson"
}
}
//create actor
val actor = transaction {
Actor.new {
firstname = "Daisy"
lastname = "Ridley"
}
}
//add reference
transaction {
film.actors = SizedCollection(listOf(actor))
}
Suppose one were to add extra information to this relation, such as the role played, then the current solution would be to have a Role
-like entity, referencing the film and actor, and containing the extra data. Now you have a problem referencing Actor
using the via
keyword. Wouldn't the following approach be preferable? Why is this not possible?
// The StarWarsFilmActors table now contains a role field
object StarWarsFilmActors : Table() {
val starWarsFilm = reference("starWarsFilm", StarWarsFilms).primaryKey(0)
val actor = reference("actor", Actors).primaryKey(1)
val roleName = varchar("50")
}
transaction {
film.actors.push(actor, {roleName = "Rey"})
}