Search code examples
swiftfluentvapor

Swift Fluent MySQL Migration to create LONGTEXT column


I am using Vapor and Fluent to build services that use a MySQL database. However, I can't figure out how to make migrations create a field with .string type that produces a column that is not VARCHAR.

Here is a simplified code snippet, for what it's worth:

        try await database.schema("log_entries")
            .id()
            .field("field", .string, .required)
            .field("value", .string, .required) <-- the problem is here - don't want varchar
            .field("changed_by", .int64, .required)
            .field("created_at", .date, .required)
            .create()

As it stands, all string values are VARCHAR(256). I get that I can change the size of the VARCHAR, but the value field should not be a VARCHAR at all, but TEXT.

I've dipped into the source code for migrations, and into the code for he MySQL driver, but I've not found anything. I know it breaks the agnostic nature of Fluent, but it's pretty important to get the column types right for the specific db.

Is there a way to specify TEXT or JSON in a Fluent MySQL migration?


Solution

  • You can pass custom types if you need something that Fluent doesn't support:

    .field("value", .custom(SQLRaw("TEXT")), .required)
    

    The underlying driver should convert it fine when saving/retrieving.