I am following this wiki to learn kotlin Exposed. It contains an example of Referencing
using Star Wars data. I would like to create a scratch and/or console file in IntelliJ which reproduces this simple example. However When I try to run the code I get errors (see below). Is there something I am missing here?
Note that when there is only one table StarWarsFilm
i am able to successfully add the record theLastJedi
. The below errors only show when i have added referencedOn
links between the tables.
Errors
error: unresolved reference: UserRating
val ratings by UserRating referrersOn UserRatings.film
error: unresolved reference: UserRatings
val ratings by UserRating referrersOn UserRatings.film
error: unresolved reference: StarWarsFilm
var film by StarWarsFilm referencedOn UserRatings.film // use referencedOn for normal references
error: unresolved reference: starWars
starWars()
error: unresolved reference: StarWarsFilm
val theLastJedi = StarWarsFilm.new {
Entered into an IntelliJ scratch console
import com.zaxxer.hikari.HikariConfig
import com.zaxxer.hikari.HikariDataSource
import io.ktor.server.application.*
import org.jetbrains.exposed.dao.IntEntity
import org.jetbrains.exposed.dao.IntEntityClass
import org.jetbrains.exposed.dao.id.EntityID
import org.jetbrains.exposed.dao.id.IntIdTable
import org.jetbrains.exposed.sql.Database
import org.jetbrains.exposed.sql.SchemaUtils
import org.jetbrains.exposed.sql.transactions.transaction
fun main(args: Array<String>): Unit =
io.ktor.server.netty.EngineMain.main(args)
///////////////////////////////////////////////////////////////////////////////////////
object StarWarsFilms : IntIdTable() {
val sequelId = integer("sequel_id").uniqueIndex()
val name = varchar("name", 50)
val director = varchar("director", 50)
}
class StarWarsFilm(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<StarWarsFilm>(StarWarsFilms)
var sequelId by StarWarsFilms.sequelId
var name by StarWarsFilms.name
var director by StarWarsFilms.director
val ratings by UserRating referrersOn UserRatings.film
}
///////////////////////////////////////////////////////////////////////////////////////
object Users : IntIdTable() {
val name = varchar("name", 50)
}
class User(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<User>(Users)
var name by Users.name
}
///////////////////////////////////////////////////////////////////////////////////////
object UserRatings : IntIdTable() {
val value = long("value")
val film = reference("film", StarWarsFilms)
val user = reference("user", Users)
}
class UserRating(id: EntityID<Int>) : IntEntity(id) {
companion object : IntEntityClass<UserRating>(UserRatings)
var value by UserRatings.value
var film by StarWarsFilm referencedOn UserRatings.film // use referencedOn for normal references
var user by User referencedOn UserRatings.user
}
object DatabaseFactory {
fun init() {
fun hikari(): HikariDataSource {
val config = HikariConfig()
config.driverClassName = "org.postgresql.Driver" // 1
config.jdbcUrl = "jdbc:postgresql:star_wars?user=postgres&password=xxxx" // 2
config.maximumPoolSize = 3
config.isAutoCommit = false
config.transactionIsolation = "TRANSACTION_REPEATABLE_READ"
config.validate()
return HikariDataSource(config)
}
Database.connect(hikari())
transaction {
SchemaUtils.create(
StarWarsFilms,
UserRatings,
Users
)
}
}
}
@Suppress("unused")
fun Application.module() {
DatabaseFactory.init()
starWars()
}
fun Application.starWars() {
transaction {
val theLastJedi = StarWarsFilm.new {
name = "The Last Jedi"
sequelId = 36
director = "Rian Johnson"
}
val someUser = User.new {
name = "Some User"
}
val rating1 = UserRating.new {
value = 9
user = someUser
film = theLastJedi
}
val rating2 = UserRating.new {
value = 6
user = someUser
film = theLastJedi
}
}
}
println("end")