Emanuele Iannuzzi
03/20/2025, 6:24 PMUser
in the Users
table (either with the User.new
DAO method or the Users.insert
method), an enumeration field accountstatus
that is described as non-null, with a default value, makes the insertion fail as of `null value in column accountstatus
violates not-null constraint` .
Here is the code for the table definition and the user DAO:
object Users : UUIDTable() {
val email = varchar("email", 255).uniqueIndex()
val role = enumeration<Role>("role")
val registrationDate =
datetime("registrationDate").default(Clock.System.now().toLocalDateTime(TimeZone.currentSystemDefault()))
val disabledDate = datetime("disabledDate").nullable()
val accountStatus = enumeration<AccountStatus>("accountStatus").default(AccountStatus.ACTIVE)
enum class Role {
PROFESSOR, STUDENT
}
enum class AccountStatus {
ACTIVE, DISABLED
}
}
class User(id: EntityID<UUID>) : UUIDEntity(id), UserData {
companion object : UUIDEntityClass<User>(Users)
override var email by Users.email
override var role by Users.role
override val registrationDate by Users.registrationDate
override var disabledDate by Users.disabledDate
override var accountStatus by Users.accountStatus
val sessions by Session referrersOn Sessions.userId
}
Here is the code that throws the exception:
// ...
transaction {
User.new {
email = email /* logic omitted */
role = role /* logic omitted */
}
}
Moreover, could someone tell me if there is a way, even if client-side and not database-enforced, to construct default values based on properties of the soon-to-be inserted row? I've read about the clientDefault
method but I can't seem to figure out how to build a default value based on the various Column
s.
Thanks in advance, and sorry for the poor English.Emanuele Iannuzzi
03/23/2025, 12:20 PM