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 迁移.
这篇关于运行迁移时出现重复列名错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:运行迁移时出现重复列名错误


基础教程推荐
- CHECKSUM 和 CHECKSUM_AGG:算法是什么? 2021-01-01
- 从字符串 TSQL 中获取数字 2021-01-01
- while 在触发器内循环以遍历 sql 中表的所有列 2022-01-01
- MySQL根据从其他列分组的值,对两列之间的值进行求和 2022-01-01
- 带更新的 sqlite CTE 2022-01-01
- 带有WHERE子句的LAG()函数 2022-01-01
- ORA-01830:日期格式图片在转换整个输入字符串之前结束/选择日期查询的总和 2021-01-01
- 如何在 CakePHP 3 中实现 INSERT ON DUPLICATE KEY UPDATE aka upsert? 2021-01-01
- 使用 VBS 和注册表来确定安装了哪个版本和 32 位 2021-01-01
- MySQL 5.7参照时间戳生成日期列 2022-01-01