Kotlin 暴露 - 没有标识列的表的实体

Oxy*_*ron 6 sql postgresql primary-key kotlin kotlin-exposed

我可以在 Kotlin 公开库上找到的所有材料都假设该表有一个主标识列,因此在大多数示例中显示的实体继承了IntEntity抽象类。例如:

class UserLocation(id: EntityID<Int>) : IntEntity(id) {
    companion object : IntEntityClass<UserLocation>(UserLocations)

    var userId by UserLocations.userId
    var user by User referencedOn UserLocations.user
    var recordedAt by UserLocations.recordedAt
    var insertedAt by UserLocations.insertedAt
    var point by UserLocations.point
Run Code Online (Sandbox Code Playgroud)

这对应于这个表定义:

object UserLocations : IntIdTable("user_locations") {

    val userId = integer("user_id")
    val user = reference("user_id", Users)
    val recordedAt = datetime("recorded_at").nullable()
    val insertedAt = datetime("inserted_at")
    val point = point("point")
}
Run Code Online (Sandbox Code Playgroud)

问题是,我的表实际上没有标识列。我知道没有主键的表的所有负面影响,但不幸的是我只能读取该表。我无法获得对该表的写访问权以添加主键。

在公开库的 wiki 中,它提到对象表定义可以继承TableIntIdTable不是IntEntity.

我的问题:当表没有 id 列时,我的实体应该继承(而不是IntEntity)什么。

Tap*_*pac 6

暴露的 DAO 需要一些身份来从表中获取和存储实体。它不需要是主列或自动增量列,但在这种情况下,您必须确保该列中的值是唯一的。

例如,您可以将 Entity 映射到带有 String id 的表:

object FooTable : IdTable<String>() {
    val myIdColumn = varchar("my_id_column", 50).uniqueIndex()
    override val id: Column<EntityID<String>> = myIdColumn.entityId()
}

class FooEntity(id: String) : Entity<String>(id) {
    companion object : EntityClass<String, FooEntity>(FooTable)
}
Run Code Online (Sandbox Code Playgroud)