2

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?

asked Feb 18, 2025 at 2:34

1 Answer 1

2

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.

answered Feb 18, 2025 at 17:00
Sign up to request clarification or add additional context in comments.

3 Comments

That gives me Cannot convert value of type 'SQLRaw' to expected argument type 'DatabaseSchema.DataType'. I've looked for a DataType case that accepts a SQLRaw as an argument, but I didn't find one.
This worked for me: .field("value", .custom(SQLRaw("TEXT")), .required) - if you tweak your answer I will mark it as correct.
Ah never write code from memory! Fixed

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.