【问题标题】:vapor 3, fluent model doesn't create raw in DB蒸汽 3,流利模型不会在 DB 中创建原始模型
【发布时间】:2018-10-01 17:37:54
【问题描述】:

我正在尝试创建一个模型(MySQLStringModel)并为其设置一个 id(String ? 类型)。控制台中没有错误或任何其他消息,save(on: req) 工作成功,但模型未出现在数据库的表中。有什么问题吗?

func create(_ req: Request, person: Person) throws -> Future<Person> {
    return Person(id: person.id, name: person.name).save(on: req)
}

P.s.:当我使用带有Int? id 的 MySQLModel 并且我不直接设置此 id 时,一切正常(它使用 autoincreament 设置自动)

【问题讨论】:

    标签: vapor


    【解决方案1】:

    方法的问题 .save(on: req)

    如文档中所述: Saves the model, calling either create(...) or update(...) depending on whether the model already has an ID. If you need to create a model with a pre-existing ID, call create instead.

    所以,.create(on: req) 方法帮助了我,新的 raw 出现在表格中。

    【讨论】:

    • 注意:save(…) 当前实现与 Vapor 文档一致……但是 save(…) 有两个意想不到的限制。 [1] 没有错误或反馈表明该实例实际上并未写入数据库。 [2] save(…) 仅根据 Swift 端的状态创建(...)或更新(...)(不考虑数据库的实际状态)。理想情况下,恕我直言,save(…) 或类似的将是更了解并提供 SQL UPDATE OR INSERTUPSERT 等行为。见https://github.com/vapor/fluent-sqlite/issues/23
    猜你喜欢
    • 2019-03-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-08-05
    • 2020-10-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多