Search code examples
mysqlruby-on-railsrails-migrationsrails-activestorageruby-on-rails-5.2

Why does active_storage:install's migration (v5.2.1) generate invalid MySQL syntax when run?


I've upgraded a Rails 4.x app to v5.2.1. The upgrade went fine and the app runs well. However, I've now run rails active_storage:install, which creates a migration to create the tables for ActiveStorage.

The migration is created fine, and looks okay. However, when running rails db:migrate, I get:

Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'string NOT NULL, `filename` string NOT NULL, `content_type` string, `metadata` t' at line 1: CREATE TABLE `active_storage_blobs` (`id` integer NOT NULL AUTO_INCREMENT PRIMARY KEY, `key` string NOT NULL, `filename` string NOT NULL, `content_type` string, `metadata` text, `byte_size` bigint NOT NULL, `checksum` string NOT NULL, `created_at` datetime NOT NULL, UNIQUE INDEX `index_active_storage_blobs_on_key`  (`key`)) ENGINE=InnoDB

So apparently, it is translating t.string to "CREATE TABLE (name string) <-- "string", and not varchar or something. Why is that?

When I manually run this CREATE TABLE with varchars instead of 'string', the table is created fine.


Solution

  • Okay, I solved it: it was caused by a monkey-patch overriding sql_type, that was necessary for utf8mb4 in Rails 4.x to fix the primary key type. I have removed that initializer, and migrations work now. Thanks to everyone who spent time thinking about it.