2017-09-22 3 views
0

ich eine Tabelle erstellen, indemWarum kann ich nicht auf mein Datenbankschema migrieren Index

Schienen mit Modell Beziehung erzeugen follower_id: integer followed_id: integer

Dann habe ich meinen Index hinzufügen, wie

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 
folge

Danach lief ich rake db: migrate rake db:migrate

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

-- create_table(:relationships) 

-> 0.0010s 

== 20170922165845 CreateRelationships: migrated (0.0011s) ===================== 

Warum migriert Index nicht?

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

Diese migrierte ... woher wissen Sie, dass die Indizes nicht funktionierten? – user3366016

+0

Es gibt nichts im Schema –

+0

Gibt es Ihre DB und Ihre Datenbank.yml? – user3366016

Antwort

0

lief ich Ihre Befehle in einem brandneuen Schienen (Version 4.2 und Ruby 2.3.4) Projekt und alles funktioniert wie erwartet. Ich würde rake db:rollback und löschen Sie die Dateien und versuchen Sie es erneut.

Meine Schritte:

Ich lief rails generate model Relationship follower_id:integer followed_id:integer

Dann die neu angelegte Migration bearbeitet Indizes hinzufügen wie folgt:

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 

lief rake db:migrate geöffnet schema.rb und hatte HINWEIS Die Indizes liegen außerhalb des create_table Blocks:

# 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

Immer noch nicht funktioniert bro. Ich bin kein Programmierer, ich versuche nur zu lernen –

Verwandte Themen