fluent icon indicating copy to clipboard operation
fluent copied to clipboard

Is it possible to create longer strings than `.string` datatype on databases?

Open betzerra opened this issue 1 year ago • 4 comments

Today I got a MySQL error: Server error: Data too long for column 'tasting_notes' at row 1 error.

I assume a .string dataType has a limited size. My schema is defined as:

  func prepare(on database: Database) -> EventLoopFuture<Void> {
      database.schema("coffee")
          .id()
          .field("name", .string, .required)
          .field("country_code", .string, .required)
          .field("process", .string)
          .field("varietal", .string)
          .field("tasting_notes", .string)
          .create()
  }

Is there a way to specify a data type longer than a .string?

betzerra avatar Oct 15 '24 19:10 betzerra

I was thinking on defining .data and then handling myself the encoding / decoding similar to this issue https://github.com/vapor/fluent/issues/439 but this looks overly complicated

betzerra avatar Oct 15 '24 19:10 betzerra

I'm assuming the generated table is VARCHAR(256)? You can provide a custom type, like

.field("tasting_notes", .custom(SQLRaw("VARCHAR(1024)")))

0xTim avatar Oct 15 '24 19:10 0xTim

@0xTim this is great! Thank you!

I guess I can wrap that into something like

if let _ = database as? MySQLDatabase {

so I can keep compatibility between my production environment (MariaDB) and my local environment (SQLite)

betzerra avatar Oct 17 '24 16:10 betzerra

I mean you can do but I really would recommend using the same DB locally and in production - we've seen way too many issues with people using different DBs

0xTim avatar Oct 17 '24 18:10 0xTim