2017-09-22 28 views
0

にインデックスを移行することはできませんなぜ私は私は私のデータベーススキーマ

レールを使用してテーブルを生成するには、モデルの関係follower_idを生成:整数followed_id:整数

を次に

class CreateRelationships < ActiveRecord::Migration 
    def change 
    create_table :relationships do |t| 
     t.integer :follower_id 
     t.integer :followed_id 

     t.timestamps null: false 
    end 
    add_index :relationships, :follower_id 
    add_index :relationships, :followed_id 
    add_index :relationships, [:follower_id, :followed_id], unique: true 
    end 
end 
を次のように、私は私のインデックスを追加します

その後、私はすくいデシベルを実行しました: rake db:migrate

== 20170922165845 CreateRelationships: migrating ============================== 

-- create_table(:relationships) 

-> 0.0010s 

== 20170922165845 CreateRelationships: migrated (0.0011s) ===================== 
を移行

インデックスを移行しないのはなぜですか?

schema.rb

ActiveRecord::Schema.define(version: 20170922181915) do 
    #... 
    create_table "relationships", force: :cascade do |t| 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 
    #... 
end 
+1

この移行された...インデックスが機能しなかったことをどのように知っていますか? – user3366016

+0

スキーマには何もありません –

+0

あなたのDBは存在し、あなたのdatabase.ymlはありますか? – user3366016

答えて

0

私は、ブランドの新しいレール(バージョン4.2およびルビー2.3.4)プロジェクトにあなたのコマンドを実行し、すべてが期待どおりに動作します。私はrake db:rollbackとファイルを削除し、もう一度試してみます。

マイステップ:

私はその後rails generate model Relationship follower_id:integer followed_id:integer

を走ったが、次のようにインデックスを追加するために、新しく作成された移行を編集した:

class CreateRelationships < ActiveRecord::Migration 
    def change 
    create_table :relationships do |t| 
     t.integer :follower_id 
     t.integer :followed_id 

     t.timestamps null: false 
    end 
    add_index :relationships, :follower_id 
    add_index :relationships, :followed_id 
    add_index :relationships, [:follower_id, :followed_id], unique: true 
    end 
end 

rake db:migrate を走っschema.rbを開き、NOTICEを持っていましたインデックスはcreate_tableブロックの外にあります

# encoding: UTF-8 
# This file is auto-generated from the current state of the database. Instead 
# of editing this file, please use the migrations feature of Active Record to 
# incrementally modify your database, and then regenerate this schema definition. 
# 
# Note that this schema.rb definition is the authoritative source for your 
# database schema. If you need to create the application database on another 
# system, you should be using db:schema:load, not running all the migrations 
# from scratch. The latter is a flawed and unsustainable approach (the more migrations 
# you'll amass, the slower it'll run and the greater likelihood for issues). 
# 
# It's strongly recommended that you check this file into your version control system. 

ActiveRecord::Schema.define(version: 20170922191347) do 

    create_table "relationships", force: :cascade do |t| 
    t.integer "follower_id" 
    t.integer "followed_id" 
    t.datetime "created_at", null: false 
    t.datetime "updated_at", null: false 
    end 

    add_index "relationships", ["followed_id"], name: "index_relationships_on_followed_id" 
    add_index "relationships", ["follower_id", "followed_id"], name: "index_relationships_on_follower_id_and_followed_id", unique: true 
    add_index "relationships", ["follower_id"], name: "index_relationships_on_follower_id" 

end 
+0

まだ働いていません。私はコーダーではない、私はちょうど勉強しようとしている –

関連する問題