【问题标题】:All table fields are not showing? (Rails/MySQL)所有表格字段都没有显示? (导轨/MySQL)
【发布时间】:2018-06-22 05:06:46
【问题描述】:

我在模型生成的迁移文件中创建了新字段“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

但是,一旦我显示来自的字段,在本例中为“用户”

mysql> SHOW FIELDS FROM users;

它应该有 returns this table,但它也应该返回 first_name、last_name、email 和密码。

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

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

【问题讨论】:

  • 您进行了迁移吗?

标签: ruby-on-rails database migration ruby-on-rails-5 rails-models


【解决方案1】:

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

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

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,它应该有这些字段。

【讨论】:

    【解决方案2】:

    您一定错过了运行迁移。

    从项目的根目录运行这个 cmd

    bundle exec rails db:migrate # for rails 5
    

    bundle exec rake db:migrate
    

    【讨论】:

      猜你喜欢
      • 2014-02-06
      • 1970-01-01
      • 2019-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多