Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
betzerra opened this issue Oct 15, 2024 · 4 comments

Comments

@betzerra
Copy link

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 betzerra changed the title Is it possible to create longer strings on databases? Is it possible to create longer strings than .string datatype on databases? Oct 15, 2024
@betzerra
Copy link
Author

I was thinking on defining .data and then handling myself the encoding / decoding similar to this issue #439 but this looks overly complicated

@0xTim
Copy link
Member

0xTim commented Oct 15, 2024

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

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

@betzerra
Copy link
Author

@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)

@0xTim
Copy link
Member

0xTim commented Oct 17, 2024

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants