【问题标题】:MySQL Fulltext index with Rails 2.3.2 (migration problem)Rails 2.3.2 的 MySQL 全文索引(迁移问题)
【发布时间】:2009-04-17 07:57:40
【问题描述】:

我在 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`)

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

谢谢!

我的迁移:

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"

【问题讨论】:

    标签: mysql sql ruby-on-rails mysql-error-1170


    【解决方案1】:

    我认为你需要在你的 environment.rb 中设置这个:

    config.active_record.schema_format = :sql
    

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

    【讨论】:

    • 这并不能解决我的问题...我在 environment.rb 中有该行,并且正在尝试在迁移中创建索引。它给出了与 OP 相同的错误。
    【解决方案2】:

    使用一个可以轻松插入 Rails 的全文搜索引擎怎么样?省去你用mysql自己做的麻烦。提供大量自定义的两个不错的选择是:

    【讨论】:

    • 好主意,但如果我想自己处理这个问题,我该怎么办?
    • 使用 sphinx(与 TS 一起使用时 是一个非常好的选择),您还必须处理通过 cron 作业保持守护程序运行并重建索引的问题。我将尝试在一个小项目中使用原生 MySQL 方法。
    猜你喜欢
    • 1970-01-01
    • 2015-03-01
    • 1970-01-01
    • 2018-10-30
    • 2010-11-01
    • 1970-01-01
    • 2015-08-30
    • 2016-10-13
    • 1970-01-01
    相关资源
    最近更新 更多