问题描述
如何通过 Vapor 定义约束?这是我最近的尝试:
database.schema(Thingmabob.schema)
.id()
.field("parentID",.uuid,.required,.references(Stuff.schema,.id))
.field("name",.string,.required)
.field("isDocumened",.bool,.required)
.field("value1",.uint,.required)
.field("value2",.uint)
.constraint(.custom("value2 > value1 + 1")) // << "Syntax error"
.create()
解决方法
解决方案有两个:
1) 似乎是文档 — 哈! — 忘了提到 .constraint(.custom( ... ))
中提到的任何字段都设置为小写。因此,如果您有像 .constraint(.custom("isDocumented ...")
这样的字段/列,则约束中的值变为 isdocumented
,它无法匹配任何列。只是漂亮!
2) 另外,请注意,当我使用 .constraint()
API 时,框架不知道如何处理它...因此我必须编写完整的语句 CHECK notcamelcase > alsonotcamelcase
(不仅仅是 notcamelcase > alsonotcamelcase
)。不知道为什么不将 .constraint()
命名为 .arbitrarySQLSegment()
,因为它比我读过的所有文档都更准确和有用。