MySQL Fulltext index with Rails 2.3.2 (migration problem)
Asked Answered
L

2

7

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"
Loki answered 17/4, 2009 at 7:57 Comment(0)
G
5

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

Guay answered 16/5, 2009 at 18:26 Comment(1)
This doesn't solve the problem for me... I have that line in environment.rb and am trying to create an index in a migration. It gives the same error as the OP.Undecided
C
1

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:

Chaperone answered 18/4, 2009 at 14:55 Comment(2)
Good idea, but in case I want to deal with this myself, what could I do?Loki
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.Penniepenniless

© 2022 - 2024 — McMap. All rights reserved.