的ActiveRecord :: PendingMigrationError

时间:2015-03-29 16:58:23

标签: ruby-on-rails sqlite

我不知道该怎么做。当我尝试rake db:migrate时。我在下面得到这个

$ rake db:migrate 
DL is deprecated, please use Fiddle
== 20150329164142 CreateCarts: migrating     ======================================
-- create_table(:carts)
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

SQLite3::SQLException: table "carts" already exists: CREATE TABLE "carts" ("id" INTEGER PRIMARY KEY AU
TOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL) c:/Work/depot/db
/migrate/20150329164142_create_carts.rb:3:in `change'
c:in `migrate'
ActiveRecord::StatementInvalid: SQLite3::SQLException: table "carts" already exists: CREATE TABLE "carts" ("id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "created_at" datetime NOT NULL, "updated_at" datetime NOT NULL)
c:/Work/depot/db/migrate/20150329164142_create_carts.rb:3:in `change'
c:in `migrate'
SQLite3::SQLException: table "carts" already exists
c:/Work/depot/db/migrate/20150329164142_create_carts.rb:3:in `change'
c:in `migrate'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

我对编程很新,我尝试了bin/rake db:migrate RAILS_ENV=developmentrake db:rollback。似乎没什么用。我尝试删除购物车文件夹并再次启动生成脚手架流程,迁移仍然是一个问题。请帮忙。

1 个答案:

答案 0 :(得分:1)

您似乎正在尝试创建已存在的表carts。 解决此问题的一种方法是创建一个新的迁移,负责删除表,然后我将运行一个新的迁移来创建carts表。

首先,rails g migration drop_table_carts

class DropTableCarts < ActiveRecord::Migration
    def change
      drop_table :carts  
    end
end

然后运行新的迁移rails generate migration create_table_carts <column_name>:<column_type>

查看您的迁移文件夹,您应该看到create_carts迁移文件:

class CreateCarts < ActiveRecord::Migration
  def change
    create_table :carts do |t|
    end
  end
end

最后,rake db:migrate。瞧!这应该照顾你的问题