登录

在 Fluent 中创建字段时,有办法添加 comment 注解吗?

fade 2022-07-05 18:27:36


1个回答

2

accept

Vapor migrate没有官方支持comment方法,不过还是可以自己写的。

postgresql

根据postgresql的语法我们可以这样做。

import Fluent
import FluentSQL

struct CreateTodo: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema("todos")
            .id()
            .field("title", .string, .required)
            .create()

            if let sql = database as? SQLDatabase {
                   try await sql.raw("comment on column todos.id is '主键'").run()
            }
    }

    func revert(on database: Database) async throws {
        try await database.schema("todos").delete()
    }

}

mysql

mysql应该更简单一点

import Fluent
import FluentSQL

struct CreateTodo: AsyncMigration {
    func prepare(on database: Database) async throws {
        try await database.schema("todos")
            .id()
            .field("title", .string, .required, .custom("comment '主键'"))
            .create()
    }

    func revert(on database: Database) async throws {
        try await database.schema("todos").delete()
    }

}

2022-07-05 23:07:54

更多