```class UserEntity(id: EntityID<Long>) : Lo...
# exposed
r
Copy code
class UserEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<UserEntity>(UserTable)
    var username by UserTable.username
}

class TestEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<TestEntity>(TestTable)
    var author by UserEntity referencedOn TestTable.autor
//    var author by TestTable.author // not this way
}

transaction {
    TestEntity.all().first().author // triggers fetch of entire entity
    TestEntity.all().first().readValues[TestTable.author] // returns the desired id but is a little awkward
}
I want to benefit from the dao api by fetching entire related entites (like author here). However, there are some use cases when the id of the referenced entity is sufficient and I don't want to cause another database query to fetch the other unneeded columns. The only - but awkward - way I found to achieve this was by using "readValues". Is there any better solution that I have overlooked?
1
h
What is wrong with
var authorID by TestTable.author // or better val
? This works if
authorID
is a FK.
r
Then I lost the ability to fetch the entire UserEntity (in case I need more than just the id). That way I can solely access the value of the foreign key.
h
You could simple add both properties
r
I'm quite sure I tried. It must have said smth like "duplicate column names for 'author'"
h
This sounds strange. We use this syntax in almost every entity class:
Copy code
class TestEntity(id: EntityID<Long>) : LongEntity(id) {
    companion object : LongEntityClass<TestEntity>(TestTable)

    var author by UserEntity referencedOn TestTable.autor
    var authorID by TestTable.author
}
r
Okay, Im gonna try this out again straight away. Maybe I did a careless mistake.
Well... it works. For some reason I assumed that it doesn't work. Thank you, sometimes you just need another person that can look at the situation without bias.
h
Yeah, or a duck 😄
r
That's hilarious, I will definitely get myself one.