d

    Deji

    9 months ago
    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?
    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!
    dave08

    dave08

    9 months ago
    You need an EntityID instance there, not just an Int
    d

    Deji

    9 months ago
    thanks! how do I create an entity ID instance here?
    dave08

    dave08

    9 months ago
    it[externalProfileId] = EntityID(profile[id], Profile)
    d

    Deji

    9 months ago
    thanks for the help Dave!