带有Rails 2.3.2的MySQL全文索引(迁移问题)

时间:2021-01-08 22:22:08

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.):

我在Rails 2.3.2应用程序中使用MySQL全文索引。我在迁移中通过本机SQL添加了索引。但是有一个已知的问题导致schema.rb出现问题。 Rails不了解全文索引并尝试创建普通索引。从schema.rb创建数据库时会出错(例如测试,规格等):

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?

有没有办法在没有猴子修补Rails的情况下在Rails 2.3.2中解决这个问题?如果没有,最好的方法是什么?

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"

2 个解决方案

#1


5  

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

我想你需要在你的环境中设置它.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

这里的参考:https://rails.lighthouseapp.com/projects/8994/tickets/74-problem-with-tests-and-custom-mysql-fulltext-indexes

#2


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:

如何使用其中一个可以轻松插入Rails的全文搜索引擎?使用mysql可以省去自己的麻烦。提供大量定制的两个好选择是:

#1


5  

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

我想你需要在你的环境中设置它.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

这里的参考:https://rails.lighthouseapp.com/projects/8994/tickets/74-problem-with-tests-and-custom-mysql-fulltext-indexes

#2


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:

如何使用其中一个可以轻松插入Rails的全文搜索引擎?使用mysql可以省去自己的麻烦。提供大量定制的两个好选择是: