如何使用 Vapor 添加约束?

问题描述

如何通过 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(),因为它比我读过的所有文档都更准确和有用。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...