rake db migrate aborted:重复列名称(HELP!)

时间:2014-02-16 03:29:59

标签: ruby-on-rails activerecord gem rake dbmigrate

我已经运行了“$ rails generate paperclip pin image”但决定重新安装它以尝试解决“无法运行Identify命令。请安装ImageMagick”。上传图片时出错。

因此去了终端并运行“$ rails generate paperclip pin image --force”但遇到了我无法解决的问题。请帮助!

Steven-Lims-MacBook-Pro:Pinteresting stevenlim$ rake db:migrate
==  AddAttachmentImageToPins: migrating =======================================
-- change_table(:pins)
rake aborted!

发生了错误,此操作和所有后​​续迁移都已取消:

SQLite3::SQLException: duplicate column name: image_file_name: ALTER TABLE "pins" ADD "image_file_name" varchar(255)/usr/local/rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `initialize'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `new'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:91:in `prepare'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/sqlite3-1.3.8/lib/sqlite3/database.rb:134:in `execute'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `block in execute'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `block in log'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activesupport-4.0.2/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract_adapter.rb:430:in `log'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:331:in `execute'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:360:in `add_column'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/sqlite3_adapter.rb:454:in `add_column'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_definitions.rb:360:in `column'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/paperclip-3.5.4/lib/paperclip/schema.rb:52:in `block (2 levels) in attachment'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/paperclip-3.5.4/lib/paperclip/schema.rb:51:in `each_pair'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/paperclip-3.5.4/lib/paperclip/schema.rb:51:in `block in attachment'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/paperclip-3.5.4/lib/paperclip/schema.rb:50:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/paperclip-3.5.4/lib/paperclip/schema.rb:50:in `attachment'
/Users/stevenlim/Desktop/pinteresting/db/migrate/20140216024257_add_attachment_image_to_pins.rb:4:in `block in up'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/schema_statements.rb:334:in `change_table'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:625:in `block in method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `block in say_with_time'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:597:in `say_with_time'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:617:in `method_missing'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:383:in `method_missing'
/Users/stevenlim/Desktop/pinteresting/db/migrate/20140216024257_add_attachment_image_to_pins.rb:3:in `up'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:534:in `up'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:574:in `exec_migration'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:555:in `block (2 levels) in migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:554:in `block in migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/connection_pool.rb:294:in `with_connection'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:553:in `migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:709:in `migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:959:in `block in execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `block in ddl_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `block in transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:210:in `within_new_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/connection_adapters/abstract/database_statements.rb:202:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/transactions.rb:209:in `transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:1005:in `ddl_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:958:in `execute_migration_in_transaction'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:920:in `block in migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `each'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:916:in `migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:764:in `up'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/migration.rb:742:in `migrate'
/usr/local/rvm/gems/ruby-2.0.0-p353/gems/activerecord-4.0.2/lib/active_record/railties/databases.rake:42:in `block (2 levels) in <top (required)>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

STATUS

Steven-Lims-MacBook-Pro:Pinteresting stevenlim $ rake db:migrate:status

数据库:/Users/stevenlim/Desktop/pinteresting/db/development.sqlite3

 Status   Migration ID    Migration Name
--------------------------------------------------
   up     20140205132449  Devise create users
   up     20140210143217  Create pins
   up     20140212145812  Add user id to pins
   up     20140215161350  ********** NO FILE **********
  down    20140216134900  Add attachment image to pins

3 个答案:

答案 0 :(得分:1)

ImageMagickPaperclip gem的先决条件。仅当您的系统上安装了ImageMagick并且Paperclip可以访问它时,Paperclip才会起作用。查看您的错误"Could Not Run The Identify Command. Please Install ImageMagick.",似乎您的系统上未安装ImageMagick。第一步是安装它或检查它是否已安装在您的环境配置文件中,让Paperclip通过向其添加ImageMagick路径知道在哪里寻找ImageMagick。

例如: 在开发模式下,您可以将此行添加到config/environments/development.rb

Paperclip.options[:command_path] = write_your_imagemagick_path

其中  您可以通过运行which convert

来获取ImageMagick路径

查看您遇到的第二个错误,似乎您之前已经运行过一次迁移,因此,已经在数据库表image_file_name中创建了与pins相关的回形针附件字段。当您尝试再次运行该迁移时,您将收到重复的列名称错误。

EDIT:

第一次生成paperclip pin image时,在db/migrations下创建了一个迁移文件,其中查看了我假设您使用rake db:migrate运行的错误,这就是为什么您已经在pins table中有与Paperclip图像相关的列。

稍后当您第二次生成paperclip pin image时,会创建另一个具有相同列名的迁移文件,当您使用rake db:migrate运行时,这显然会产生duplicate column error

您可以删除此第二个迁移文件,因为已经在数据库中创建了列,所以您不需要运行它。

但是如果您仍想运行第二个迁移文件,那么首先执行rake db:rollback直到您运行第一次迁移的步骤,以便从数据库中删除与图像相关的列。然后删除第一个迁移文件并运行rake db: migrate这将运行第二个迁移文件,并在pins table中为您创建与图像相关的列。

答案 1 :(得分:1)

我无法发表评论(因为我必须有50个代表)。您需要做的就是从数据库中的schema_migrations表中删除该编号。

答案 2 :(得分:1)

@baloo,@ user3317140 - 感谢您的帮助。经过多次试验和错误,我设法解决了这个问题,希望这能帮助其他人面临同样的问题。

要删除迁移ID: 20140215161350 Migration name: ********** NO FILE **********,我使用code class CreateNothing < ActiveRecord::Migration def change end end.

创建了20140215161350_create_nothing.rb

这允许找到迁移名称,然后我运行rake:db:rollback.

我做了另一个rake:db:回滚$ rails generate paperclip pin image.

现在全部清洁。回到ImageMagick问题。