gpt4 book ai didi

android - 我可以将非主键字段作为房间中自动生成的值吗

转载 作者:太空宇宙 更新时间:2023-11-03 13:39:54 25 4
gpt4 key购买 nike

我有一个包含复合主键的表。我可以将其中一个文件设为自动生成吗?

场景:我有一个包含字段 techerId、姓名、性别、角色、部门、年级的 teacher 表,它具有由角色、部门和年级组成的复合主 ID。如何让teacherid自动生成?

最佳答案

如果你通过THIS ANSWER ,你会发现我们不能在 Composite Primary keys 中有 auto increment 属性。因此,解决方法是使用 indexingunique constraint 的概念。将 roledepttgrade 列设置为 indices 并设置 unique约束为 true。这将确保这三个值对始终是唯一的(如 primaryKey)。然后在实体中添加 techerId 作为 Primarykey(autoGenerate = true)。您的 entity 类看起来像这样:

@Entity(tableName = "teacher",indices = arrayOf(Index(value = ["role","department","grade"],unique = true)))
public class Teacher{
@PrimaryKey(autoGenerate = true)
int teacher_id;

@ColumnInfo(name = "name")
String teacher_name;

//Rest of the fields
......
......
}

关于android - 我可以将非主键字段作为房间中自动生成的值吗,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54667625/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com