railsでmigrationを行ってもschema.rbに外部キーが作られた形跡が見られません。
*前提
現在railsでTwitterライクなアプリケーションを作っておりまして、ユーザーは複数の投稿をするという機能を実装しています。
*分からない箇所
migrationファイルで外部キーの設定をしてもschema.rbに反映されていないことに詰まっています。
本来ならschema.rbの一番下に外部キー一覧が表示されると思うのですが、私のファイルはされていません。
*対象のファイル
db/migration/create_users.rb
ruby
1class CreateUsers < ActiveRecord::Migration[5.2] 2 def change 3 create_table :users do |t| 4 t.string :name 5 t.string :email 6 t.integer :age 7 8 t.timestamps 9 end 10 end 11end 12
db/migration/create_posts.rb
ruby
1class CreatePosts < ActiveRecord::Migration[5.2] 2 def change 3 create_table :posts do |t| 4 t.text :content, null: false 5 t.references :user, foreign_key: true 6 t.string :image, null: false 7 8 t.timestamps 9 end 10 add_index :posts, [:user_id, :created_at] 11 end 12end
db/schema.rb
ruby
1ActiveRecord::Schema.define(version: 2019_08_17_020505) do 2 3 create_table "posts", force: :cascade do |t| 4 t.text "content", null: false 5 t.integer "user_id", null: false 6 t.string "image" 7 t.datetime "created_at", null: false 8 t.datetime "updated_at", null: false 9 t.index ["user_id", "created_at"], name: "index_posts_on_user_id_and_created_at" 10 t.index ["user_id"], name: "index_posts_on_user_id" 11 end 12 13 create_table "users", force: :cascade do |t| 14 t.string "name", null: false 15 t.string "email", null: false 16 t.datetime "created_at", null: false 17 t.datetime "updated_at", null: false 18 t.string "password_digest", null: false 19 t.string "intro", null: false 20 t.boolean "admin", default: false 21 t.string "image" 22 end 23 24end 25
*試したこと
追加で以下のようなmigrationファイルを作成してmigrateしましたが、結果は変わりませんでした。
ruby
1class AddUserRefToPost < ActiveRecord::Migration[5.2] 2 def change 3 add_foreign_key :posts, :users 4 end 5end
*環境
rails 5.2
以上、もし分かる方いらっしゃいましたら、ご回答よろしくお願いします。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/08/20 12:01