JetBrains / Exposed

Kotlin SQL Framework
http://jetbrains.github.io/Exposed/
Apache License 2.0
8.38k stars 694 forks source link

Many-to-Many mapping with attributes #1163

Open ZronekM opened 3 years ago

ZronekM commented 3 years ago

Hello, I have the following problem which I do not know how to solve. I have "Candidates" as well as "Skills" which own have their own table and I want a many to many relationship between them where the "level" is an attribute of the table which links both Entities:

object Candidates: UUIDTable() {
    val name = varchar("name", 50).index()
    val age = short("age").nullable()
    val created = datetime("created")
    val updated = datetime("updated")
}

class Candidate (id: EntityID<UUID>) : UUIDEntity(id) {
    companion object : UUIDEntityClass<Candidate>(Candidates)
    var name by Candidates.name
    var age by Candidates.age
    var skills by Skill via CandidateSkills
}

object CandidateSkills: Table() {
    val candidate = reference("candidate", Candidates, onDelete = ReferenceOption.CASCADE).primaryKey(0)
    val skillName = reference("skillName", Skills, onDelete = ReferenceOption.CASCADE).primaryKey(1)
    val level = short("level")
}

object Skills: UUIDTable() {
    val skillName = varchar("skill_name", 40).index()
}

class Skill(id: EntityID<UUID>) : UUIDEntity(id) {
    companion object : UUIDEntityClass<Skill>(Skills)

    var skillName by Skills.skillName
    // How get this to work?
    var level by CandidateSkill referencedOn CandidateSkills.level
}

The documentation does not mention how to reference such an attribute.

Could somebody help me please?

Best regards. Mike

1fexd commented 3 years ago

Hi, I have had the same problem in the past, but my issue @ #928 has not been addressed. Kind of sad actually, since this is a really good ORM, but without basic SQL functionality it is not suitable at all for larger projects, so I suggest you switch to something else if you plan on doing more complex things than having a few int-id-PK tables.

kapstahillar commented 2 years ago

Has anyone found a workaround for it yet? Earlier today i said nice things about JetBrains, but now seeing that ORM system cannot do basic thing like that, makes me think another way. It has been a year from last comment...