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

Timur Atakishiev

01/05/2020, 5:15 PM
Hi all
Copy code
object Users : Table("users") {
    override val primaryKey = PrimaryKey(columns = *arrayOf(Column<Long>(Users, "id", LongColumnType())))
    val id = long("id").autoIncrement()
    val phoneNumber = varchar("phone_number", 20).uniqueIndex()
    val firstName = varchar("first_name", 25).nullable()
    val lastName = varchar("last_name", 30).nullable()
    val password = varchar("password", 150)
}
can someone help please, when I overriding primary key as
Copy code
override val primaryKey = PrimaryKey(columns = *arrayOf(id))
IDE says me that I need to initialize the ID field, maybe I am doing something wrong? Or is it other way to declare a Primary key?
s

SackCastellon

01/06/2020, 9:42 AM
You need to override the
primaryKey
after you have declared the columns you are going to use as your primary key:
Copy code
object Users : Table("users") {
    val id = long("id").autoIncrement()
    val phoneNumber = varchar("phone_number", 20).uniqueIndex()
    val firstName = varchar("first_name", 25).nullable()
    val lastName = varchar("last_name", 30).nullable()
    val password = varchar("password", 150)

    override val primaryKey = PrimaryKey(id)
}
I usually just put the
primaryKey
at the end.
👍 1
t

Timur Atakishiev

01/06/2020, 10:14 AM
Okay, thank you, I will try