【发布时间】:2022-01-01 12:39:27
【问题描述】:
我有一个使用 Vapor Fluent PostgreSQL 构建的已部署后端。
现在我需要将新的必填字段键 new_field 添加到数据库中架构名称为 schema_name 的表中。
我创建了一个新的迁移:
struct AddNewFieldToSchema: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("schema_name")
.field("new_field", .string, .required)
.update()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("schema_name")
.deleteField("new_field")
.update()
}
}
但是运行后却报错:
Fatal error: Error raised at top level: server: column "new_field" of relation "schema_name" contains null values (ATRewriteTable)
我猜这是因为使用旧数据模型创建的现有数据没有new_field 的值,因为如果数据库为空,此新迁移运行良好。
我该如何解决这个问题?谢谢。
【问题讨论】:
标签: swift postgresql fluent vapor