运行迁移时出现重复列名错误

Duplicate column name error when running migration(运行迁移时出现重复列名错误)

本文介绍了运行迁移时出现重复列名错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每当我在我的 Rails 应用程序中运行迁移时,我都会收到来自 SQLite3 的错误:

Whenever I run a migration in my Rails app, I get an error from SQLite3:

SQLite3::SQLException: duplicate column name: photo_file_name: ALTER TABLE "users" ADD "photo_file_name" varchar(255)

我已经进行了向用户添加照片"迁移.这里是:

I already have a "Add Photo to User" migration. Here it is:

class AddAttachmentPhotoToUsers < ActiveRecord::Migration
   def self.up
     change_table :users do |t|
     t.has_attached_file :photo
    end
   end

  def self.down
   drop_attached_file :users, :photo
  end
end

这是用户迁移:

class CreateUsers < ActiveRecord::Migration
  def change
    create_table :users do |t|
    t.string :name
    t.string :title
    t.string :department
    t.text :skills
    t.boolean :available

    t.timestamps
   end
 end
end

我对此有点困惑,因为它告诉我有一个重复的列名photo_file_name",但我需要将它添加到用户表中?那没有意义.我不需要删除它吗?

I'm a bit confused by it because it's telling me there is a duplicate column name "photo_file_name" but that I need to add it to the Users table? That doesn't make sense. Shouldn't I need to remove it?

如果您需要有关我的应用的任何其他详细信息,请告诉我.

Let me know if you need any other details about my app.

推荐答案

如果您的迁移与数据库架构不同步,就会发生这种情况.如果

That happens if you migrations are not in sync with the database schema. This could happen if

  • 您手动"修改了数据库架构
  • 您更改了正在运行的迁移文件
  • schema_migrations 表中的迁移尚未更新
  • you modified the database schema "by hand"
  • you changed a migration file being run
  • migrations have not been updated in the schema_migrations table

如果您依赖数据库中的数据,rake db:reset 将从头开始重新运行所有迁移.否则,您必须通过添加到 schema_migrations 表中,将冲突的迁移识别为已运行.

If you are not relying on the data in the database, a rake db:reset would re-run all migrations from scratch. Otherwise you have to make the conflicting migration recognized as already-run by adding to the schema_migrations table.

另请参阅 RailsGuides 迁移.

这篇关于运行迁移时出现重复列名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:运行迁移时出现重复列名错误

基础教程推荐