【问题标题】:rake aborted! Mysql2::Error: Duplicate column name 'slug': ALTER TABLE `categories`耙中止! Mysql2::Error: Duplicate column name 'slug': ALTER TABLE `categories`
【发布时间】:2017-04-24 05:14:56
【问题描述】:

最近我在我的 local rails 应用程序的两个模型上实现了friendly_id。我创建了两个迁移add_slug_to_categoriesadd_slug_to_services。并且确实休息了这些步骤并让friendly_id urls 工作。然后我将相同的迁移更改推送到我的生产服务器。然后在生产 Rails 控制台中,我运行 Category.find_each(&:save)Service.find_each(&:save) 并让它在生产中也能正常工作。

然后,为了在我的本地机器上进行一些测试,我从生产环境中获取了dump.sql,并通过运行rake db:drop 并由rake db:create 创建并运行rake db:migrate 将现有数据库删除到我的本地。然后我将dump.sql 推送到本地数据库。然后当我运行rails s 时,我得到了migrations pending 错误。所以我继续运行rake db:migrate。然后我得到了

== 20170411073744 AddSlugToCategories: migrating ==============================
-- add_column(:categories, :slug, :string)
rake aborted!
StandardError: An error has occurred, all later migrations canceled:

Mysql2::Error: Duplicate column name 'slug': ALTER TABLE `categories` ADD `slug`  

我尝试删除数据库并再次创建很多次。但我一直遇到同样的问题。有人可以告诉我如何解决这个问题吗?

【问题讨论】:

  • 是的,我尝试使用rails g migration RemoveSlugFromCategories slug:string,然后再次运行rake db:migrate。遇到同样的错误。
  • 命令是ALTER TABLE categoriesDROP COLUMN slug ??
  • 难道没有 Rails 方法可以做到这一点吗?
  • 每次从生产环境中导入dump.sql 时都必须这样做吗?
  • 现在可以使用了。但是正如我所问的,每次我从生产中导入dump.sql 时都必须这样做吗?

标签: mysql ruby-on-rails activerecord


【解决方案1】:

解决当前问题需要在dbconsole中运行ALTER TABLE categories DROP COLUMN slug;,以后需要先导入dump.sql再迁移rake db:migrate

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    • 1970-01-01
    • 2011-02-06
    • 2020-02-27
    • 2019-02-05
    • 2021-11-03
    • 1970-01-01
    相关资源
    最近更新 更多