First, I've searched everywhere but couldn't find a forum / detailed guide on Exposed framework so I'm resorting to post an issue here.
How do you model a many-to-many relationship using the DAO (Entities) method? Take these models:
package abcd
import org.jetbrains.exposed.dao.*
import org.jetbrains.exposed.sql.*
object Tags : IntIdTable(name = "tags") {
val name = text("name")
}
object Posts : IntIdTable(name = "posts") {
val uploadDate = datetime("upload_date")
}
object PostsTags : Table(name = "posts__tags") {
val post = reference("post", Posts).primaryKey(0)
val tag = reference("tag", Tags).primaryKey(1)
}
class Tag(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Tag>(Tags)
var name by Tags.name
}
class Post(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Post>(Posts)
var uploadDate by Posts.uploadDate
val tags by Tag referrersOn PostsTags.tag // Obviously does not work. But how else?
}
How can I modify the Post entity so it has a tags property returning all tags associated to it through PostsTags? val tags by Tag referrersOn PostsTags.tag obviously doesn't work, and I couldn't find any similar alternative constructs.
In case such a scenario is not supported, how could one implement it? By looking on Referrers implementation I've got a general grasp on how to do it, but I'm struck at how to return a SizedIterable of tags from the results of a native SQL DSL query. Is there any mapping from SizedIterable<ResultRow> to SizedIterable<Entity>?
Maybe this will help:
val tags by Tag via PostsTags
Seems I was looking at it from the wrong direction. English is not my mother tongue, I had assumed via to meant something else altogether. Thank you very much.
I could send a pull request extending the current DAO samples to show via usage. Would it be desirable?
Ofc it will be great to improve documentations and samples. Will wait for your PR.
Most helpful comment
Maybe this will help:
val tags by Tag via PostsTags