所有 table 字段都没有显示? (Rails/MySQL)

All table fields are not showing? (Rails/MySQL)

我在模型生成的迁移文件中创建了新字段 "first_name"、"last_name"、"email" 和 "password",如下所示:

class CreateUsers < ActiveRecord::Migration[5.2]

   def up
     create_table :users do |t|
        #added fields
        t.string "first_name", :limit => 25
        t.string "last_name", :limit => 50
        t.string "email", :default => '', :null => false
        t.string "password", :limit => 40

        t.timestamps
   end
  end

  def down
    drop_table :users
  end

end

但是,一旦我显示来自的字段,在这种情况下 "users"

mysql> SHOW FIELDS FROM users;

它 returns this table 它应该有,但它应该返回 first_name、last_name、电子邮件和密码。

此外,schema.rb文件也排除了这些。

我不知道为什么它没有实现这些字段。我也是 Rails 的新手,非常感谢有关标题编辑的建议,以更好地适应我这里的情况,因为我不确定术语是否正确。谢谢!

您一定错过了 运行 次迁移。

运行 这个命令来自你项目的根目录

bundle exec rails db:migrate # for rails 5

bundle exec rake db:migrate

从共享的描述来看,您似乎首先迁移了没有列的 table,然后您必须在迁移文件中添加了列。

因此,当您将 table 迁移到数据库时,迁移文件中不存在这些列,因此您需要按照下面提到的过程来克服这种情况:

1) 使用

检查迁移状态
rake db:migrate:status

create_users 迁移必须具有版本号

2) 现在使用以下命令向下迁移:

rake db:migrate:down VERSION="version_number_of_the_create_users_migration"

3) 重复步骤 1,结果应该是 create_users 迁移现在已关闭。

4) 现在使用

进行迁移
rake db:migrate #it will migration all the down migrations
rake db:migrate:up VERSION="version_number_of_the_create_users_migration" #it will migrate the specific migration

5) 现在检查 schema.rb 它应该有那些字段。