Rails and SQLite3: Default Values

Here’s another rails fix. This one is for SQLite3 >= 3.3.8. SQLite decided to return sql strings for the default values shown in table definitions. This broke rails, the rails people got mad and won’t introduce a workaround. Well here’s my workaround.

sqlite_fix.rb:

module ActiveRecord
  module ConnectionAdapters
    class SQLiteAdapter < AbstractAdapter
      def columns table_name, name=nil
        table_structure(table_name).map do |field|
          /^'?(.*)'?$/.match field['dflt_value']
          field['dflt_value'] = $1
          SQLiteColumn.new(field['name'], field['dflt_value'], field['type'], field['notnull'] == "0")
        end
      end
    end
  end
end

Include it at the end of environment.rb. That’s it!