Hey there! Is it possible to access attributes con...
# exposed
m
Hey there! Is it possible to access attributes contained in a many-to-many relationship in Exposed? In particular, I have the following classes:
Copy code
class ShiftDao(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<ShiftDao>(Shifts)

    var date by Shifts.date
    var startTime by Shifts.startTime
    var endTime by Shifts.endTime
    var text by Shifts.text
    var isEvent by Shifts.isEvent

    var client by ClientDao optionalReferencedOn Shifts.clientId
    var schedule by ScheduleDao referencedOn Shifts.scheduleId
    var assignees by EmployeeDao via ShiftAssignees
    
}
Copy code
object ShiftAssignees : IntIdTable() {

    val shiftId = reference("shift_id", Shifts, fkName = "fk_shiftass_shift_id")
    val employeeId = reference("employee_id", Employees, fkName = "fk_shiftass_employee_id")

    val attended = bool("attended").default(true)
    
}
My goal is to get not only all EmployeeDao's out of the assignments but also the "attended" attribute. Is this possible?
c
Hi @Marc A way to get additional columns back is currently in the works. It would not be necessary to create an arbitrary id column for the link table (or require it to be an
IdTable
). Please consider tracking EXPOSED-320 for updates on completion.
m
So there's no way to reference the ShiftAssignees within the ShiftDao?
c
I've seen users resort to a few workarounds, for example creating a fake id as the table constraint then creating an associated entity class. Instead of using
via
, each field would be treated like a regular reference etc, but it doesn't seem very straightforward. An alternative I experimented with, before starting the design for the feature, involves using
CompositeIdTable
to define the link table. With that you could define an entity
ShiftAssigneesDAO
class with a single
attended
field and still use
via
in
ShiftDAO
, as well as an additional field for
attended
. The only caveat with this approach is that the
via
field (
assignees
) could only be used for retrieiving/getting. Instances of
ShiftAssigneesDAO
would have to be created as usual with
new()
and a
CompositeID
of the 2 reference column values. Here's an example of this approach in the tests.