Search code examples
rubysequel

change Sequel Column String Size


I'd like to change a String colums' size with a migration.

I tried

Sequel.migration do
  up do
    alter_table(:users) do
      set_column_type :car_model, :string, size: 30
    end
  end

  down do
  end
end

but that gives me a SQL syntax error migrating:

Sequel::DatabaseError: 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(30) NULL' at line 1

Any way to do this? So currently the field is a String of size 20, I'd like to make it a String of size 30.

Thanks in advance!

UPDATE I also tried

set_column_type :car_model, char(30)

that however results in me getting a String :car_model, :size=>255 column :/


Solution

  • You were almost correct. You need to use String instead of :string:

    set_column_type :car_model, String, size: 30
    

    Note that that uses varchar(30), not char(30). In general, unless you know you want char, you are better off with varchar.

    Alternatively, anytime you want to force a database-specific type, you can just specify it as a string:

    set_column_type :car_model, 'char(30)'