当我使用 create 向数据库添加模型时,当违反其中一个外键时,Vapor fluent 不会引发异常。它也没有插入,创建函数只是正常返回。
这是 Vapor Fluent 的标准行为吗?
我正在使用带有 PostgreSQL 的 Vapor 3。
这是我添加外键约束的迁移:
struct AddAddressForeignKeys: Migration {
typealias Database = PostgreSQLDatabase
static func prepare(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.update(Address.self, on: conn) { builder in
builder.reference(from: \.regionId, to: \CodeRegion.id)
builder.reference(from: \.countryId, to: \CodeCountry.id)
}
}
static func revert(on conn: PostgreSQLConnection) -> Future<Void> {
return PostgreSQLDatabase.update(Address.self, on: conn) { builder in
builder.deleteReference(from: \.regionId, to: \CodeRegion.id)
builder.deleteReference(from: \.countryId, to: \CodeCountry.id)
}
}
}
更新:我已经添加了 Jacob Relkin 的答案中提到的 enableReferences,但仍然没有抛出异常。H
// Configure database
let config = PostgreSQLDatabaseConfig(hostname: "localhost", port: 5432, username: "postgres", database: "test", password: nil, transport: .cleartext)
let postgres = PostgreSQLDatabase(config: config)
// Register the configured database to the database config.
var databases = DatabasesConfig()
databases.add(database: postgres, as: .psql)
databases.enableReferences(on: .psql)
services.register(databases)