首页 > 解决方案 > 如何将索引添加到 postgres 表?

问题描述

我正在尝试使用 Vapor 向 PostgreSQL 表添加列索引。我找到了几个这样做的教程,但这些代码片段都不适用于当前版本。

标签: postgresqlvapor

解决方案


据我所知,用 Fluent 创建索引是不可能的。在我的 Vapor3 项目中,我使用自己的小扩展和原始查询 https://gist.github.com/MihaelIsaev/f6442bf3698572cd9170114f236c47c2

你可以像这样使用它

extension CarBrand: Migration {
    public static func prepare(on connection: Database.Connection) -> Future<Void> {
        return Database.create(self, on: connection) { builder in
            try addProperties(to: builder)
        }.flatMap { _ in
            return connection.addIndexes(\CarBrand.addedByUser, \CarBrand.createdAt)
        }
    }
}

希望能帮助到你 :)


推荐阅读