Hello guys, i'm having an issue with referencing a...
# exposed
e
Hello guys, i'm having an issue with referencing a foreign key. Given this code
Copy code
object Views : Table() {
        val id = varchar("id", 48)
        val name = varchar("name", 128)
        val owner = varchar("owner", 48)
        val published = bool("published")
        val game = text("game")
        val featured = bool("featured")

        override val primaryKey = PrimaryKey(id)
        override val tableName: String = "views"
    }

object CharactersView : Table() {
        val characterId = long("character_id")
        val viewId = varchar("view_id", 48).references(
            Views.id, onDelete = ReferenceOption.CASCADE
        )

        override val tableName = "characters_view"
    }
I'm getting this exception
Copy code
Could not initialize class com.kos.views.repository.ViewsDatabaseRepository$CharactersView
everytime i try to use CharactersView. Works perfectly without the references. Im using latest version (0.57)
d
Is there more to that exception?
c
Hi @Eric Lopez The table object (with a foreign key) is failing because of how the table name is being overridden and how initialization steps line-by-line. If you want to provide your own explicit table names instead of Exposed generated names, then you have 2 options to make the table initialize properly: Option 1 Provide an argument to the
Table.name
property instead:
Copy code
object CharactersView : Table("characters_view") {
    val characterId = long("character_id")
    val viewId = varchar("view_id", 48).references(
        Views.id, onDelete = ReferenceOption.CASCADE
    )
}
Option 2 Move the override to the start of the block (at least before
references()
is invoked):
Copy code
object CharactersView : Table() {
    override val tableName = "characters_view"
    val characterId = long("character_id")
    val viewId = varchar("view_id", 48).references(
        Views.id, onDelete = ReferenceOption.CASCADE
    )
}
I'll take a further look into how we can avoid this dependency on our end.
d
@Chantal Loncle Would seem like
tableName
should be final rather than open then.
e
worked like a charm, thanks!
👍 1