1
0

More than 1 year has passed since last update.

【Rails】コンソール内でmigrateする方法

Posted at

環境

Rails 6.0.1
Ruby 2.6.3
PostgreSQL 11.16

コンソール内でmigrateする方法

コンソールでUserを作成しようとした際にエラーでできず、最終的にコンソール内でmigrateするのに時間がかかったので備忘録にする。

> User.create(name: "山田次郎")
=> ActiveRecord::StatementInvalid (PG::UndefinedTable: ERROR:  relation "users" does not exist)

> User
=> User(Table doesn't exist)

> rails db:migrate
=> NameError (undefined local variable or method `migrate' for main:Object)
> class CreateUsers < ActiveRecord::Migration[6.0]
>   def change
>     create_table :user do |t|
>       t.string :name

>       t.timestamps
>     end
>   end
> end

> ActiveRecord::Migration.run(CreateUsers)
-- create_table(:users)
   -> 0.0132s
=> [CreateUsers]

これでUser.createすることができた。

参考

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0