Vapor 4 无法使用 Put HTTP 请求更新可选父项
Vapor 4 Unable to Update Optional Parent with Put HTPP Request
我有一个名为管辖权的模型,其中包含政治管辖权。现在有司法管辖区可以位于其他司法管辖区内,所以我有一个可选的父管辖区...
现在我可以很好地创建新的管辖区,但是如果我尝试使用放置请求来更新管辖区父级,我会收到以下错误:
FluentKit/OptionalParent.swift:20: Fatal error: OptionalParent relation OptionalParent<Jurisdiction, Jurisdiction>(key: parent_jurisdiction_id) is get-only.
2022-05-18 13:39:12.421882-0700 Run[32583:5555712] FluentKit/OptionalParent.swift:20: Fatal error: OptionalParent relation OptionalParent<Jurisdiction, Jurisdiction>(key: parent_jurisdiction_id) is get-only.```
辖区模型
import Fluent
import Vapor
final class Jurisdiction: Model, Content {
static let schema = "jurisdictions"
@ID(key: .id)
var id: UUID?
@OptionalParent(key: "parent_jurisdiction_id")
var jurisdiction: Jurisdiction?
@Field(key: "name")
var name: String
@Field(key: "scope")
var scope: String
@Children(for: \.$jurisdiction)
var jurisdictions: [Jurisdiction]
init() { }
init(name:String, scope:String,parentJurisdictionID:UUID? = nil) {
self.name = name
self.scope = scope
self.$jurisdiction.id = parentJurisdictionID
}
}
辖区迁移
import Foundation
import Vapor
import Fluent
import FluentPostgresDriver
struct CreateJurisdiction: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.id()
//parent fields
//attribute fields
.field("name", .string, .required)
.field("scope",.string, .required)
//make unique on:
.unique(on: "name", "scope")
//create scheme
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions").delete()
}
}
struct AddParentJurisdictionToJurisdictions: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.field("parent_jurisdiction_id", .uuid, .references("jurisdictions", "id"))
.update()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.deleteField("parent_jurisdiction_id")
.delete()
}
}
管辖权控制者
import Foundation
import Vapor
import Fluent
import FluentPostgresDriver
struct JurisdictionsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let jurisdictions = routes.grouped("jurisdictions")
jurisdictions.put(use: update)
}
}
//
func update(req:Request) throws -> EventLoopFuture<HTTPStatus> {
let jurisdiction = try req.content.decode(Jurisdiction.self)
return Jurisdiction.find(jurisdiction.id,on: req.db)
.unwrap(or:Abort(.notFound))
.flatMap {
[=13=].name = jurisdiction.name
[=13=].scope = jurisdiction.scope
[=13=].jurisdiction = [=13=].jurisdiction //if I remove this line it works
return [=13=].update(on: req.db).transform(to: .ok)
}
}
}
使用如下所示的 put http 请求:http://localhost:8080/jurisdictions 包含以下内容会产生错误...不知道该怎么做。
{
"scope": "locasl",
"id": "0C6D5F6E-0244-4ABE-847F-2AF89CA27C30",
"jurisdiction": {
"id": "128133B0-25FE-4B6C-B211-CE01AA236AF9"
},
"name": "local"
}
对于 Fluent 和关系,如果您尝试设置属性,则需要通过 属性 包装器而不是 属性 本身来设置。所以在你的情况下应该是
[=10=].$jurisdiction.id = jurisdiction.id
这允许您仅使用父 ID 来更新关系,而不需要整个父 ID,就像初始化程序一样。
我有一个名为管辖权的模型,其中包含政治管辖权。现在有司法管辖区可以位于其他司法管辖区内,所以我有一个可选的父管辖区...
现在我可以很好地创建新的管辖区,但是如果我尝试使用放置请求来更新管辖区父级,我会收到以下错误:
FluentKit/OptionalParent.swift:20: Fatal error: OptionalParent relation OptionalParent<Jurisdiction, Jurisdiction>(key: parent_jurisdiction_id) is get-only.
2022-05-18 13:39:12.421882-0700 Run[32583:5555712] FluentKit/OptionalParent.swift:20: Fatal error: OptionalParent relation OptionalParent<Jurisdiction, Jurisdiction>(key: parent_jurisdiction_id) is get-only.```
辖区模型
import Fluent
import Vapor
final class Jurisdiction: Model, Content {
static let schema = "jurisdictions"
@ID(key: .id)
var id: UUID?
@OptionalParent(key: "parent_jurisdiction_id")
var jurisdiction: Jurisdiction?
@Field(key: "name")
var name: String
@Field(key: "scope")
var scope: String
@Children(for: \.$jurisdiction)
var jurisdictions: [Jurisdiction]
init() { }
init(name:String, scope:String,parentJurisdictionID:UUID? = nil) {
self.name = name
self.scope = scope
self.$jurisdiction.id = parentJurisdictionID
}
}
辖区迁移
import Foundation
import Vapor
import Fluent
import FluentPostgresDriver
struct CreateJurisdiction: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.id()
//parent fields
//attribute fields
.field("name", .string, .required)
.field("scope",.string, .required)
//make unique on:
.unique(on: "name", "scope")
//create scheme
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions").delete()
}
}
struct AddParentJurisdictionToJurisdictions: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.field("parent_jurisdiction_id", .uuid, .references("jurisdictions", "id"))
.update()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("jurisdictions")
.deleteField("parent_jurisdiction_id")
.delete()
}
}
管辖权控制者
import Foundation
import Vapor
import Fluent
import FluentPostgresDriver
struct JurisdictionsController: RouteCollection {
func boot(routes: RoutesBuilder) throws {
let jurisdictions = routes.grouped("jurisdictions")
jurisdictions.put(use: update)
}
}
//
func update(req:Request) throws -> EventLoopFuture<HTTPStatus> {
let jurisdiction = try req.content.decode(Jurisdiction.self)
return Jurisdiction.find(jurisdiction.id,on: req.db)
.unwrap(or:Abort(.notFound))
.flatMap {
[=13=].name = jurisdiction.name
[=13=].scope = jurisdiction.scope
[=13=].jurisdiction = [=13=].jurisdiction //if I remove this line it works
return [=13=].update(on: req.db).transform(to: .ok)
}
}
}
使用如下所示的 put http 请求:http://localhost:8080/jurisdictions 包含以下内容会产生错误...不知道该怎么做。
{
"scope": "locasl",
"id": "0C6D5F6E-0244-4ABE-847F-2AF89CA27C30",
"jurisdiction": {
"id": "128133B0-25FE-4B6C-B211-CE01AA236AF9"
},
"name": "local"
}
对于 Fluent 和关系,如果您尝试设置属性,则需要通过 属性 包装器而不是 属性 本身来设置。所以在你的情况下应该是
[=10=].$jurisdiction.id = jurisdiction.id
这允许您仅使用父 ID 来更新关系,而不需要整个父 ID,就像初始化程序一样。