저는 Ruby on Rails에서 새롭게 변경되었습니다. 나는 레일이 외래 키를 사용하는 방식을 이해하지 못한다. 나는 그것을 몇 일간 연구했지만 대답을 얻지 못했다.외래 키로 파괴하지 않기
간단한 예제 :
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.string :title
t.text :content
t.timestamps null: false
end
end
end
class CreateComments < ActiveRecord::Migration
def change
create_table :comments do |t|
t.string :author
t.text :content
t.references :post, index: true, foreign_key: true
t.timestamps null: false
end
end
end
내 모델은 다음과 같습니다 :
나는 두 테이블을 생성class Post < ActiveRecord::Base
has_many :comments
end
class Comments < ActiveRecord::Base
belongs_to :post
end
내 의심의 여지가있다 : 내 테이블 코멘트에서 외래 키 (가지고. reference : post, index : true, foreign_key : true) 나는 그와 관련된 COMMENTS가있는 게시물을 파기 할 수 없을 것이라고 생각하지 않습니까?
나는 위와 같이했으나 관련 의견이있는 경우에도 게시물을 삭제할 수 있습니다. 어떻게 치료할 수 있습니까? 내가 도대체 뭘 잘못하고있는 겁니까?
건배
을 또한 SQL LITE를 사용할 때 FK가 내 .schema를 확인하면 t가 생성 된 것처럼 보입니다. – Costa
sqlite> .schema posts CREATE TABLE "posts"("ID"INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, "ti tle "varchar,"content "텍스트,"created_at "datetime NOT NULL,"updated_at "datetime NOT NULL); – Costa
CREATE TABLE "comments"("id"INTEGER PRIMARY KEY AUTOINCREMENT NULL이 아닌 "author"varchar, "content"텍스트, "post_id"정수, "created_at"datetime NOT NULL, "updated_at"datetime NOT NULL); CREATE INDEX "index_comments_on_post_id"ON "comments"("post_id"); – Costa