我可以将非主键字段作为房间中自动生成的值吗
Can i make non-primary key field as autogenerated value in room
我有 table 复合主键。我可以将其中一个文件设为自动生成吗?
场景:
我有一个包含字段 techerId、姓名、性别、角色、部门、年级的 teacher table
,它具有由角色、部门和年级组成的复合主 ID。如何让teacherid自动生成?
如果你通过,你会发现我们不能在复合主键中自动增加属性。因此,解决方法是使用 indexing
和 unique constraint
的概念。将 role
、deptt
、grade
列设置为 indices
,并将 unique
约束设置为 true
。这将确保这三个值对始终是唯一的(如 primaryKey)。然后在实体中添加 techerId
作为 Primarykey
(autoGenerate = true)。您的 entity
class 看起来像:
@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
......
......
}
我有 table 复合主键。我可以将其中一个文件设为自动生成吗?
场景:
我有一个包含字段 techerId、姓名、性别、角色、部门、年级的 teacher table
,它具有由角色、部门和年级组成的复合主 ID。如何让teacherid自动生成?
如果你通过indexing
和 unique constraint
的概念。将 role
、deptt
、grade
列设置为 indices
,并将 unique
约束设置为 true
。这将确保这三个值对始终是唯一的(如 primaryKey)。然后在实体中添加 techerId
作为 Primarykey
(autoGenerate = true)。您的 entity
class 看起来像:
@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
......
......
}