rake db:migrate 没有创建用户 table

rake db:migrate is not creating user table

我有以下迁移

问题是 rake db:migrate 没有执行第一次迁移并且没有创建 users table。

这可能是什么原因?

What could be the reason for this?

主要原因可能是您已经 运行 迁移 - 或者可能是以后的迁移 - 因此 Rails 认为不需要 运行 它。

查看是否属于这种情况的一个好方法是打开您的 db/schema.rb 文件:

您会看到您的架构是 运行ning 的最新迁移。如果这取代了您尝试调用的那个,它将不会 运行.

--

修复

可以生成新的迁移,然后将代码复制过来:

$ rails g migration AddUsers2

然后添加以下内容:

#db/migrate/_____.rb
class AddUsers2 < ActiveRecord::Migration
   def change
      create_table :users do |t| 
        t.string :name
        t.timestamps
      end
   end
end

或者,您可以擦除数据库并重新开始。这可以使用 rake schema:load 来实现。 这将清除所有数据并重新开始