2011-01-09 3 views
1

Padrino를 DataMapper와 함께 사용하고 있으며 모델에 연결을 추가하기 위해 마이그레이션을 시도하고 있습니다. 예를 들어,이로 시작 :DataMapper Association Migrations

class User 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 
end 

class Post 
    include DataMapper::Resource 

    property :id, Serial 
    property :title, String 
    property :body, Text 
end 

class Comment 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 
end 

그리고 다음과 같이 종료 :

class User 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 

    has n, :posts 
end 

class Post 
    include DataMapper::Resource 

    property :id, Serial 
    property :title, String 
    property :body, Text 

    belongs_to :user 
    has n, :comment 
end 

class Comment 
    include DataMapper::Resource 

    property :id, Serial 
    property :name, String 

    belongs_to :post 
end 

나는 이미 세 개의 테이블을 만들기위한 마이그레이션을 가지고,하지만 난 협회를 추가하지 않습니다. 연결을 위해 마이그레이션을 생성하기위한 코드는 무엇입니까?

답변

2

DataMapper.auto_upgrade! 새 FK 속성을 추가합니다.

+0

감사합니다. 먼저 마이 그 레이션을 추가해야한다고 생각했습니다. –

1

auto_upgrade는 좋지만 점진적으로 뒤로 물러 설 수는 없습니다.

migration 3, :create_products do 
    up do 
    modify_table :post do 
     add_column :user_id, Integer 
    end 
    modify_table :comment do 
     add_column :post_id, Integer 
    end 
    end 

    down do 
    modify_table :post do 
     drop_column :user_id, Integer 
    end 
    modify_table :comment do 
     drop_column :post_id, Integer 
    end 
    end 
end 

그게 전부입니다.