Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

add_column with non-existing type

#1434

We can add column with non-existing (or misspelled) type.


add_column :users, :enabled, :bool # misspelled - should be :boolean

Migration works fine with SQLite3, but it can't be undone:


rake db:migrate:down VERSION=20081122152613 --trace

...
You have a nil object when you didn't expect it!
The error occurred while evaluating nil.to_sym
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb:423:in `column'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:318:in `copy_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:312:in `each'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:312:in `copy_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb:98:in `create_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:310:in `copy_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:304:in `move_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:297:in `alter_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb:66:in `transaction'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:296:in `alter_table'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:228:in `remove_column'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:227:in `each'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb:227:in `remove_column'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/migration.rb:346:in `send'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/migration.rb:346:in `method_missing'
/home/kuba/edetailing/vendor/rails/activerecord/lib/active_record/migration.rb:326:in `say_with_time'
...

Reported by qoobaa · November 22nd, 2008 @ 04:07 PM

State: stale
Milestone: 2.x
Assigned to: Pratik Pratik
Importance: none

Activity

  1. qoobaa
    qoobaa
    • Tag changed from activerecord, edge, migrations, sqlite to activerecord, edge, migrations, patch, sqlite

    The patch solves problem with reversing migrations. We should also consider restricting SQLite3 types to avoid mistakes like above.

    November 22nd, 2008 @ 05:28 PM

  2. Pratik
    Pratik
    • Assigned user set to Pratik

    Shouldn't the real fix be to raise an exception when someone tries to add a column of unknown type ?

    Also, the patch is missing tests.

    Thanks.

    March 6th, 2009 @ 07:46 PM

  3. qoobaa
    qoobaa

    AFAIR (it was few months ago) the problem is that SQLite3 doesn't have any strict set of types. Restricting them is the easiest way to fix the problem but it might cause problems when somebody uses strange type names already (don't know what for :-) ).

    March 6th, 2009 @ 10:54 PM

  4. qoobaa
    qoobaa

    It's also really nasty bug. When you create such strange typed column, there's no easy way to remove it - SQLite3 doesn't support removing columns from tables. My solution was the easiest way to repair my DB.

    March 6th, 2009 @ 11:00 PM

  5. Pratik
    Pratik
    • State changed from new to stale

    May 18th, 2009 @ 10:08 PM

  6. bterkuile
    bterkuile

    I had the same problem, setting a type to :bool for sqlite3 will cause many problems. At least it should be converted to :boolean when a sqlite database is used. The problem is described at: http://kconrails.com/blog/articles/2008/07/15/ruby-on-rails-bool-vs... so I won't add much words to that, except how I resolved the problem.

    Since migrations fail when the field has type bool it has to be changed manually. Since I don't like the code to change the column type for sqlite my most elegant solution was in the console:
    ActiveRecord::Base.connection.change_column :table_name, :column_name, :boolean, :default => false

    July 29th, 2009 @ 03:53 PM