0

如何通过 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()
4

1 回答 1

0

Solution is two folds:

1) Seems the documentation — HA! — forgot to mention that any field mentionned in the .constraint(.custom( ... )) is set to lowercase. So if you have a field/column like .constraint(.custom("isDocumented ..."), the value in the constraint becomes isdocumented which fails to matches any column. Just beautiful!

2) Also, note that, while I'm using the .constraint() API, the framework has no idea what do to with it... I thus have to write the full statement CHECK notcamelcase > alsonotcamelcase (not just notcamelcase > alsonotcamelcase). Not sure why .constraint() isn't named .arbitrarySQLSegment() as that would've been more accurate and helpful than all that documentation I've read.

于 2021-01-17T18:46:32.607 回答