如何在Kotlin中实现表继承?
例如: 我有一个名为Pet的基本表,其中包含如何在Kotlin中实现表继承?,kotlin,kotlin-exposed,Kotlin,Kotlin Exposed,例如: 我有一个名为Pet的基本表,其中包含生日和名称列 我还有两个表是从这个表派生出来的,一个叫做PetDog表,列为numberofteaths,另一个叫做PetBird表,列为bikecolor 如何使用Kotlin Exposed实现这一点 或者有关于这方面的文档吗?您想要什么样的数据库和模式?它如何支持表继承?如评论中所述,关系数据库的常用方法是使用一个表和所有列或两个表(pet和dog,以某种方式将两个表中的记录相互链接) 有两个表的简单示例: create table pet (i
生日
和名称
列
我还有两个表是从这个表派生出来的,一个叫做PetDog表,列为numberofteaths
,另一个叫做PetBird表,列为bikecolor
如何使用Kotlin Exposed实现这一点
或者有关于这方面的文档吗?您想要什么样的数据库和模式?它如何支持表继承?如评论中所述,关系数据库的常用方法是使用一个表和所有列或两个表(
pet
和dog
,以某种方式将两个表中的记录相互链接)
有两个表的简单示例:
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")
}
PetDog
表应该有3列(BirthDate
,Name
,numberoftooth
)?或者它应该有两列(BirthDate
,PetId
),其中PetId
指向Pet
表中的某一行?换句话说,使用组合而不是继承。但是,我不会使宠物引用为空。;)@TimvanderLeeuw谢谢你的评论,我同意。我已将pet_id
更改为不可为空的字段。