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 迁移.
这篇关于运行迁移时出现重复列名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:运行迁移时出现重复列名错误
基础教程推荐
- SQL Server:只有 GROUP BY 中的最后一个条目 2021-01-01
- Sql Server 字符串到日期的转换 2021-01-01
- 将数据从 MS SQL 迁移到 PostgreSQL? 2022-01-01
- ERROR 2006 (HY000): MySQL 服务器已经消失 2021-01-01
- 在 VB.NET 中更新 SQL Server DateTime 列 2021-01-01
- 无法在 ubuntu 中启动 mysql 服务器 2021-01-01
- SQL Server 中单行 MERGE/upsert 的语法 2021-01-01
- 使用pyodbc“不安全"的Python多处理和数据库访问? 2022-01-01
- 如何在 SQL Server 的嵌套过程中处理事务? 2021-01-01
- SQL Server 2016更改对象所有者 2022-01-01