Add indexes to your Fluent SQL databases in Vapor

Aron Budinszky
hoursofoperation
Published in
Sep 1, 2022

--

Vapor’s Fluent ORM allows you to easily create models as Swift structs that correspond to your SQL database tables. The database schema is generally defined as a migration to automatomatically create and update your database table in accordance with your app’s models.

You will find some documentation on Vapor’s site on how to perform basic database migrations, but a lot is missing — including how to add indexes to your database tables.

Photo by Jan Antonin Kolar

Though not directly supported by Fluent, the Migrations API allows you to drop to lower-level APIs easily. As such you can use `SQLKit` to add a migration like so:

Finally, don’t forget to add the struct to your migrations:

app.migrations.add(CreateVersionsCreatedAtIndex())

--

--