可以键入 UUID 吗?
Can UUIDs be typed?
假设我们在两个表 employers
和 employees
之间有一个普通的父子关系。
employees
中的字段 employerId
实际上只能与 employers
中的 id
(以及其他表中的 employerId
)相比较。为了类型安全,我想将 employers
表示为
class Employees(tag: Tag) extends Table[Employees](tag, "employees") {
def id = column[UUID[Employees]]("id")
def employerId = column[UUID[Employers]]("employerId")
...
}
将 UUID[Employers]
列与另一个 UUID[Employers]
列以外的任何内容进行比较,或者使用除相等之外的任何比较都是荒谬的(在大多数模式中,包括我的),我想捕捉这个事实在编译时。
显然,真正的 UUID 不带类型参数,但这种需求很常见。可以吗?
正确答案似乎是 Unicorn 来自 Virtus Labs。
This library adds tools to use type-safe IDs for your classes so you can no longer join on bad id field or mess up order of fields in mappings. It also provides a way to create data access layer with methods (like querying all, querying by id, saving or deleting) for all classes with such IDs in just 4 lines of code.
假设我们在两个表 employers
和 employees
之间有一个普通的父子关系。
employees
中的字段 employerId
实际上只能与 employers
中的 id
(以及其他表中的 employerId
)相比较。为了类型安全,我想将 employers
表示为
class Employees(tag: Tag) extends Table[Employees](tag, "employees") {
def id = column[UUID[Employees]]("id")
def employerId = column[UUID[Employers]]("employerId")
...
}
将 UUID[Employers]
列与另一个 UUID[Employers]
列以外的任何内容进行比较,或者使用除相等之外的任何比较都是荒谬的(在大多数模式中,包括我的),我想捕捉这个事实在编译时。
显然,真正的 UUID 不带类型参数,但这种需求很常见。可以吗?
正确答案似乎是 Unicorn 来自 Virtus Labs。
This library adds tools to use type-safe IDs for your classes so you can no longer join on bad id field or mess up order of fields in mappings. It also provides a way to create data access layer with methods (like querying all, querying by id, saving or deleting) for all classes with such IDs in just 4 lines of code.