gpt4 book ai didi

ruby-on-rails-4 - Rake db :migrate error, 表已经存在

转载 作者:行者123 更新时间:2023-12-03 15:52:32 25 4
gpt4 key购买 nike

我不小心创建了一个不需要的迁移,所以我删除了该文件,并在尝试运行 rake db:migrate 时创建了一个新的迁移,我不断收到此错误。我正在使用 SQlite3 gem 和 Ruby on Rails 4

StandardError: An error has occurred, this and all later migrations canceled:
SQLite3::SQLException: table "categories" already exists: CREATE TABLE "categories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "category_name" varchar, "created_at" datetime NOT NULL, "updated_at" datetime NO
T NULL) D:/muse/db/migrate/20150830113519_create_categories.rb:3:in `change'
D:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "categories" already exists: CREATE TABLE "categories" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "category_name" varchar, "created_at" datetime NO
T NULL, "updated_at" datetime NOT NULL)
D:/muse/db/migrate/20150830113519_create_categories.rb:3:in `change'
D:in `migrate'
SQLite3::SQLException: table "categories" already exists
D:/muse/db/migrate/20150830113519_create_categories.rb:3:in `change'
D:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

最佳答案

当你创建一个迁移并运行它,并想要创造机会时,你必须首先回滚:

$ bin/rake db:rollback STEP=1



在这个阶段你可以删除文件或修改它并再次运行迁移_

$ bin/rake db:migrate



现在你删除了文件,你不能回滚。您需要做的是手动删除该表或创建迁移以删除该表:

$ bin/rails generate migration DropCategoriesTable



这将生成一个迁移文件。编辑它:
class DropCategoriesTable < ActiveRecord::Migration
def up
drop_table :categories
end

def down
raise ActiveRecord::IrreversibleMigration
end
end

关于ruby-on-rails-4 - Rake db :migrate error, 表已经存在,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32296380/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com