Hello everyone! I'm new to backend development and...
# ktor
m
Hello everyone! I'm new to backend development and just started using Ktor and I'm loving it so far. However, I'm currently running into an issue with creating foreign key relationships using Exposed and PostgreSQL. Here is the relevant part of my code:
Copy code
object Users : IdTable<UUID>() {
    override val id = uuid("user_id").entityId()
    val name = varchar("name", 255)
    val email = varchar("email", 255).uniqueIndex()
    val passwordHash = varchar("password_hash", 60)
}

object RefreshTokens: IdTable<UUID>() {
    override val id = uuid("refresh_token_id").entityId()
    val userId = reference("user_id", Users).uniqueIndex()
    val token = varchar("token", 255)
    val expiresAt = datetime("expires_at")
}
I'm trying to create a relationship between
Users
and
RefreshTokens
tables with
user_id
being a foreign key in
RefreshTokens
table. However, when I try to run my application, I get this error:
ERROR: there is no unique constraint matching given keys for referenced table "users"
From what I understand,
user_id
in
Users
is a primary key and it should be unique, but I'm not sure why I'm getting this error. I've also posted this question on StackOverflow. Any insights would be greatly appreciated. Thank you!
1
c
Even though you're running a Ktor server, this question has nothing to do with Ktor. You might try posting in #exposed instead
m
Got it figured out, but thanks! I'll keep that in mind for future queries.
z
For reference, for anyone else with the same problem:
Copy code
val userId = reference("user_id", Users.id).uniqueIndex()