Swift 将列添加到Vapor中的现有表中

Swift 将列添加到Vapor中的现有表中,swift,sqlite,vapor,server-side-swift,Swift,Sqlite,Vapor,Server Side Swift,我有一个我正在做的项目,上面有帖子和评论。我使用外键(postId)将评论链接到帖子。然而,直到我第一次用Comment类构建项目之后,这个外键才被添加到我的Comment类中 将postId字段添加到comment类后,我尝试运行该项目并创建一条注释。该项目构建并运行良好,但当我尝试创建注释时,会出现错误:table comment没有名为postId的列 这是Vapor中的某种迁移问题吗 您仍然需要将数据库与vapor中的更改同步。正如您所猜测的,您可以通过配置迁移来实现这一点。将其添加到c

我有一个我正在做的项目,上面有
帖子
评论
。我使用外键(postId)将评论链接到帖子。然而,直到我第一次用Comment类构建项目之后,这个外键才被添加到我的Comment类中

postId
字段添加到comment类后,我尝试运行该项目并创建一条注释。该项目构建并运行良好,但当我尝试创建注释时,会出现错误:
table comment没有名为postId的列


这是Vapor中的某种迁移问题吗

您仍然需要将数据库与vapor中的更改同步。正如您所猜测的,您可以通过配置迁移来实现这一点。将其添加到configure.swift文件中。如果之前已经创建了迁移结构,则可能需要选择不同的名称,因为相同的名称可能会导致问题

struct AddPostID: Migration {
    // Need to declare which database, assuming PostgreSQL here
    typealias Database = PostgreSQLDatabase

    static func prepare(on conn: PostgreSQLConnection) -> Future<Void> {
        return Database.update(Comment.self, on: conn) { builder in
            builder.field(for: \.postId)
        }
    }

    static func revert(on connection: PostgreSQLConnection) -> Future<Void> {
        return Database.delete(Comment.self, on: connection)
    }
}
var migrations = MigrationConfig()
migrations.add(migration: AddPostID.self, database: .psql)
services.register(migrations)