运行迁移时列名重复错误 [英] Duplicate column name error when running migration

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

问题描述

每当我在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"重复,但是我需要将其添加到Users表中吗?那没有道理.我不需要删除它吗?

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.

另请参见迁移指南.

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

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆