Swift 5.2 Xcode 11.4 Vapor 4.0.0如何在我的模型中编码PK?

问题描述 投票:2回答:1

我的package.swift看起来像:

.package(url: "https://github.com/vapor/vapor.git", from: "4.0.0-rc"),
.package(url: "https://github.com/vapor/fluent.git", from: "4.0.0-rc"),
.package(url: "https://github.com/vapor/fluent-postgres-driver.git", from: "2.0.0-rc")

我的config.swift看起来像:

app.databases.use(.postgres(
    hostname: Environment.get("DATABASE_HOST") ?? "localhost",
    username: Environment.get("DATABASE_USERNAME") ?? "postgres",
    password: Environment.get("DATABASE_PASSWORD") ?? "secret",
    database: Environment.get("DATABASE_NAME") ?? "mydb2"
), as: .psql)

我的模型看起来像:

import Fluent
import Vapor


final class Complaint: Model, Content {

    static let schema = "cw_complaint5"

    @ID(key: .id)
    var id: UUID?

    @Field(key: "issue_description")
    var issue_description: String

    // etc.
    init() { }
}

Xcode,建立OK,GET运行OK:

http://localhost:8080/complaint

但是在POST上,我进入了响应正文:

{
    "error": true,
    "reason": "Value of type 'UUID' required for key 'id'."
}

在Vapor 3中,POST可以很好地插入新行(在json请求正文中省略id)和更新现有行。 PostgreSQL 11.3上的表描述如下:

mydb2=# \d+ cw_complaint5
                                              Table "public.cw_complaint5"
        Column        |          Type           | Collation | Nullable | Default | Storage  | Stats target | Description 
----------------------+-------------------------+-----------+----------+---------+----------+--------------+-------------
 id                   | integer                 |           | not null |         | plain    |              | 
 row                  | smallint                |           |          |         | plain    |              | 
 document_number      | integer                 |           | not null |         | plain    |              |
...etc...
county_id            | integer                 |           |          |         | plain    |              | 
 city_id              | integer                 |           |          |         | plain    |              | 
 operator_id          | integer                 |           |          |         | plain    |              | 
Indexes:
    "cw_complaint5_pkey" PRIMARY KEY, btree (id)
Check constraints:
    "cw_complaint5_row_check" CHECK ("row" >= 0)
Foreign-key constraints:
    "cw_complaint5_city_id_fkey" FOREIGN KEY (city_id) REFERENCES cw_city5(id)
    "cw_complaint5_county_id_fkey" FOREIGN KEY (county_id) REFERENCES cw_county5(id)
    "cw_complaint5_operator_id_fkey" FOREIGN KEY (operator_id) REFERENCES cw_operator5(id)

(END)

失败的POST请求的请求正文(更新现有行)如下:

{"id":729,"issue_description":"test","document_number":99,"api_state_code":88,"api_county_code":11,"section":22,"city_id":51,"county_id":56,"operator_id":4415}
swift xcode postgresql vapor
1个回答
0
投票

您的问题是Complaint模型的ID类型为UUID,但是在POST请求正文中,您正在传递Int值。由于您要迁移的现有项目已经具有使用整数ID定义的数据库,因此,最好的解决方案是更改模型的ID类型。

因为Fluent 4尝试支持所有数据库,所以默认情况下它仅支持UUID ID类型。您可以使用自定义ID类型,如下所示:

@ID(custom: "id", generatedBy: .database) var id: Int?

这将使您的模型可以使用当前的数据库表结构,并在POST请求正文中使用整数ID。

您的模型最终将如下所示:

final class Complaint: Model, Content {

    static let schema = "cw_complaint5"

    @ID(custom: "id", generatedBy: .database)
    var id: Int?

    @Field(key: "issue_description")
    var issue_description: String

    // etc.
    init() { }
}
© www.soinside.com 2019 - 2024. All rights reserved.