如何在Kotlin Exposed中实现表继承?

3

示例:

我有一个基础表称为Pet,包含BirthDateName列。

我有两个从该表派生的表,一个称为PetDog表,包含NumberOfTeeth列,另一个称为PetBird表,包含BeakColor列。

如何使用Kotlin Exposed实现这个?https://github.com/JetBrains/Exposed

或者是否有相关文档可供参考?


1
PetDog表应该有3列(BirthDateNameNumberOfTeeth)吗?还是它应该只有2列(BirthDatePetId),其中PetId指向Pet表中的某一行? - Михаил Нафталь
1个回答

2
你有什么样的数据库和模式想法?它如何支持表继承?正如Михаил Нафталь在评论中提到的,使用关系型数据库的常见方法是使用一个包含所有列的表或两个表(petdog,并且有某种方法将两个表中的记录链接起来)。
以下是一个简单的示例,包括两个表:
create table pet (id int auto_increment primary key, birthdate varchar(10) not null, "name" varchar(50) not null)
create table dog (id int auto_increment primary key, pet_id int not null, number_of_teeth int not null, constraint fk_dog_pet_id_id foreign key (pet_id) references pet(id) on delete restrict on update restrict)

您的暴露表定义代码可能如下所示:

object Pet : IntIdTable() {
    val birthdate = varchar("birthdate", 10)
    val name = varchar("name", 50)
}

object Dog : IntIdTable() {
    val petId = reference("pet_id", Pet).nullable()
    val numberOfTeeth = integer("number_of_teeth")
}

1
换句话说,使用组合而非继承。不过我不会将Pet引用设为可空的。 ;) - Tim van der Leeuw
1
@TimvanderLeeuw 感谢您的评论,我同意。我已将 pet_id 更改为非空字段。 - Freek de Bruijn

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接