Exposed: EntityID<Int> are not replaceable by Int

Created on 22 Feb 2018  路  2Comments  路  Source: JetBrains/Exposed

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?

Most helpful comment

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"
}

All 2 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

gertvdijk picture gertvdijk  路  4Comments

barry-m picture barry-m  路  3Comments

supertote picture supertote  路  3Comments

gcscaglia picture gcscaglia  路  3Comments

BugsBunnyBR picture BugsBunnyBR  路  3Comments