https://kotlinlang.org logo
#exposed
Title
# exposed
d

Deji

12/19/2021, 1:39 AM
Hi, I am kotlin/exposed newbie and I am having trouble inserting into a table with a foreign key reference. Please can someone tell me what I am doing wrong?
Copy code
object Profile : IntIdTable("profile", "id") {
  val externalId: Column<String> = varchar("external_id",
    255).uniqueIndex("external_profile_external_id_uindex")

  val profileUrl: Column<String?> = varchar("profile_url",
    255).uniqueIndex("external_profile_profile_url_uindex").nullable()

  val imageUrl: Column<String?> = varchar("image_url", 255).nullable()

  val uri: Column<String> = varchar("uri", 255).uniqueIndex("external_profile_uri_uindex")
}

object User : IntIdTable("user", "id") {
  val username: Column<String> = varchar("username",
    20).uniqueIndex("internal_user_username_uindex")

  val email: Column<String> = varchar("email",
    255).uniqueIndex("internal_user_email_uindex")

  val displayName: Column<String?> = varchar("display_name", 255).nullable()

  val externalProfileId: Column<Int> =
    integer("profile_id").references(Profile.id).uniqueIndex("internal_user_external_profile_id_uindex")
}


val profile = Profile.insert {
  it[externalId] = "externalId"
  it[profileUrl] = "profileUrl"
  it[imageUrl] = "imageUrl"
  it[uri] = "uri"
}

val user = User.insert {
  it[username] = "username"
  it[email] = "email"
  it[displayName] = "displauName"
  it[externalProfileId] = profile[id] // <--- this line fails to compile
}
I cant set the externalProfileId foreign key to the ID from the new profile created. is there a better way to do this? Thanks!
d

dave08

12/19/2021, 4:02 AM
You need an EntityID instance there, not just an Int
d

Deji

12/19/2021, 10:46 PM
thanks! how do I create an entity ID instance here?
d

dave08

12/20/2021, 10:01 AM
Copy code
it[externalProfileId] = EntityID(profile[id], Profile)
d

Deji

12/20/2021, 11:47 AM
thanks for the help Dave!
g

Giuliopime

11/09/2023, 6:27 PM
chat
134 Views