This is related to the issue I raised a few days ago: https://github.com/JetBrains/Exposed/issues/250
I have utilized the answers to that one as the basis for this.
Maybe it's me that doesn't completely understand the inner workings of Exposed but,
Using the same example I used, if you have:
object Users : IntIdTable("users") {
val name = varchar("name", length = 60)
}
class User(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<User>(Users)
var name by Users.name
}
object Animals : IntIdTable("animals") {
val user = reference("userId", Users, onDelete = ReferenceOption.CASCADE)
val name = varchar("name", length = 60)
}
class Animal(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<Animal>(Animals)
var name by Animals.name
var user by User referencedOn Animals.user
var userId by Animals.user
}
you can not do something like:
val tommy = Animal.new {
userId = 2 // this has to be an EntityID<Int>
name = "Tommy"
}
Any way around this?
I don't see any reason to have both user and userId. Please keep only one, you could work with any of them:
id example:
val tommy = Animal.new {
userId = EntityID(2, Users)
// OR
userId = User.new { ... }.id
name = "Tommy"
}
reference example:
val tommy = Animal.new {
user = User[2]
// OR
user = User.new { ... }
name = "Tommy"
}
Thanks a lot @Prototik,
I guess some of these constructs should be added to the docs.
Most helpful comment
I don't see any reason to have both
useranduserId. Please keep only one, you could work with any of them:id example:
reference example: