views:

1489

answers:

2

Hi,

I'm using MySQL fulltext indexes in a Rails 2.3.2 App. I added the index via native SQL in my migration. But there is a known issue causing problems with the schema.rb. Rails doesn't understand fulltext indexes and tries to create a normal index. This will cause an error when creating the database from schema.rb (eg testing, specs, etc.):

Mysql::Error: BLOB/TEXT column 'text' used in key specification without a key length: CREATE  INDEX `fulltext_sms` ON `sms` (`text`)

Is there a way to solve this issue in Rails 2.3.2 without monkey-patching Rails? And if not, what's the best way to deals with this?

Thank you!

My migration:

class FulltextIndexCustomersSmsMessage < ActiveRecord::Migration
  def self.up
    execute('ALTER TABLE sms ENGINE = MyISAM')
    execute('ALTER TABLE customers ENGINE = MyISAM')
    execute('CREATE FULLTEXT INDEX fulltext_sms ON sms (text(500))')
    execute('CREATE FULLTEXT INDEX fulltext_customer ON customers (fullname(255))')
  end

  def self.down
    execute('ALTER TABLE sms ENGINE = innodb')
    execute('ALTER TABLE customers ENGINE = innodb')
    execute('DROP INDEX fulltext_sms ON sms')
    execute('DROP INDEX fulltext_customer ON customers')
  end
end

schema.rb:

add_index "sms", ["text"], :name => "fulltext_sms"
A: 

How about using one of the full-text search engines that can be easily plugged into Rails? Saves you the trouble of doing it all yourself with mysql. Two good options, that provide lots of customization, are:

insane.dreamer
Good idea, but in case I want to deal with this myself, what could I do?
ole_berlin
with sphinx (which *is* a very good choice when used with TS) you also have to deal with keeping the daemon running with a cron job and rebuilding the indexes. I'm going to try using the native MySQL approach on a small project.
Mike Breen
+3  A: 

I think you need to set this in your environment.rb:

config.active_record.schema_format = :sql

Here the reference: https://rails.lighthouseapp.com/projects/8994/tickets/74-problem-with-tests-and-custom-mysql-fulltext-indexes

newtonapple