如何在 mysql2+rails 3 中添加索引?

发布于 2024-10-08 01:25:18 字数 398 浏览 0 评论 0原文

mysql2中如何添加索引?

与线: 执行“更改表 urlli_development.slugs 添加索引 slugs_sluggable_id(sluggable_id)” 一切正常。

使用行: add_index :slugs, :sluggable_id 我收到此“无效日期”错误。

问题是我必须添加以下行: add_index(:slugs, [:name, :sluggable_type, :sequence, :scope], :unique => true, :name => 'index_slugs_on_n_s_s_and_s')

上面的行不起作用。

我使用 Rails 3 和 mysql2,当我尝试迁移数据库时,出现“无效日期”错误。

你怎么做?

How do you add indexes in mysql2?

with line:
execute "alter table urli_development.slugs ADD INDEX slugs_sluggable_id(sluggable_id)"
everything works fine.

with line: add_index :slugs, :sluggable_id I get this 'Invalid date' error.

Problem is that I have to add following line:
add_index(:slugs, [:name, :sluggable_type, :sequence, :scope], :unique => true, :name => 'index_slugs_on_n_s_s_and_s')

Above line doesn't work.

I use Rails 3 and mysql2 and I get this 'Invalid date' error when I try to migrate database.

How do you do it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忆离笙 2024-10-15 01:25:18

我也遇到过这个问题,坦率地说,我还没有找到完整的“为什么”。但我确实想分享一些我刚刚发现的东西。

尝试通过排除“id”字段来创建表...add_index 将立即运行。

class CreateSlugs < ActiverRecord::Migration   
    def self.up
      create_table :slugs,:id=>false do |t|             
         t.string "name"
         t.timestamps
       end
       add_index :indexes,"name",:name=>"slugs_name",:unique=>true
       add_column :slugs,:id,:primary_key
    end
end

所以或者如果您希望在不同的迁移中执行此操作

class AlterSlugs < ActiveRecord::Migration
  def self
    #add_index :slugs,:name,:unique
    #add_column :slugs,:id,:primary_key        
  end

  def self.down
  end
end

实际上非常有趣 ->如果您切换这两行(add_index 和 add_column)...它不会工作!

I've had that problem too and to be quite frankly haven't got to a complete "why".But I do want to share something I just found out.

Try creating your table by excluding the "id" field....the add_index will run right away.So

class CreateSlugs < ActiverRecord::Migration   
    def self.up
      create_table :slugs,:id=>false do |t|             
         t.string "name"
         t.timestamps
       end
       add_index :indexes,"name",:name=>"slugs_name",:unique=>true
       add_column :slugs,:id,:primary_key
    end
end

Or if you wish to do it within a different migration

class AlterSlugs < ActiveRecord::Migration
  def self
    #add_index :slugs,:name,:unique
    #add_column :slugs,:id,:primary_key        
  end

  def self.down
  end
end

Actually is pretty interesting-> If you switch those two lines (add_index and add_column) ...IT WON'T WORK!

瞳孔里扚悲伤 2024-10-15 01:25:18

您遇到了 此错误

(我假设你使用的是Windows)
您可以通过将新文件:///C:/../mysql/lib/opt/libmysql.dll 从 mysql 5.1 安装复制到 Ruby/bin 目录来解决该问题。

然后它又可以工作了!

you ran into this bug.

(I assume you're on Windows)
You can solve it by copying a fresh file:///C:/../mysql/lib/opt/libmysql.dll from your mysql 5.1 install to your Ruby/bin directory.

Then it works again!

仅此而已 2024-10-15 01:25:18

您需要将“libmysql.ddl”重新复制并粘贴到 ruby​​ 的 bin 文件夹中。

如果您的计算机上安装了 Mysql Workbench,则可以在 Mysql Workbench 的文件夹中获取它,或者从其他来源获取它。

You need to copy and paste, "libmysql.ddl" anew into ruby's bin folder.

You can get it in the folder of Mysql Workbench if you have it installed in your machine or from alternative sources.

哭泣的笑容 2024-10-15 01:25:18

尝试使用以前版本的 mysql gem 而不是 mysql2...

gem 'mysql', '2.8.1'
# gem 'mysql2'

我在两个不同的项目中遇到过这个错误两次,这在两种情况下都拯救了我的日子:)
还值得一提的是,在 Ubuntu 中使用 mysql2 时,add_index 不会造成任何问题。我想这只是 Windows 的问题。

Try using a previous version of the mysql gem instead of mysql2...

gem 'mysql', '2.8.1'
# gem 'mysql2'

I've run into this bug twice, in two different projects and this saved my day in both cases :)
It's also worth mentioning that add_index causes no trouble at all using mysql2 in Ubuntu. I guess this is a windows only issue.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文