2017-10-10 3 views
0

は、次の移行のために仮定存在文句:ActiveRecordのは存在しない(参照用)カラムを訴えるので、私はそれを手動で作成し、それは

class AddSectionReferences < ActiveRecord::Migration 
    def change 

    add_reference :sections, :sections, index: true, foreign_key: true, on_delete: :nullify 
    add_reference :sections, :parent 
    end 
end 

それは文句:そう

ActiveRecord::StatementInvalid: PG::UndefinedColumn: ERROR: column "section_id" referenced in foreign key constraint does not exist 
: ALTER TABLE "sections" ADD CONSTRAINT "fk_rails_810c69e885" 

私が追加した場合:

add_column :sections, :sections_id, :integer 

参照する前に、それは、文句を言う:

ActiveRecord::StatementInvalid: PG::DuplicateColumn: ERROR: column "sections_id" of relation "sections" already exists 
: ALTER TABLE "sections" ADD "sections_id" integer 

複数列(has_manyの場合)を作成しようとすると、最初のエラーでsection_id列を探しているのはなぜですか?

答えて

1

複数の列(has_many)を作成しようとしていますが、

これは間違ったところから近づいています。この列に複数の/無限のIDを含めるとどう思いますか?レールが物事を期待する方法ではありません。

has_manyの関係では、外部キー列はbelongs_to側にあります。列名は自然に単数でなければなりません。それは1つのIDしか保持できないからです。

t.references :section 
+0

http://guides.rubyonrails.org/association_basics.html#the-has-many-through-association OPはこの – Mark

+0

@マークに従ってください:あなたはそれが事実だと思いますか? –

+0

彼の質問の外見から彼はまだそれを読んでいない:( – Mark

関連する問題