2011-01-09 69 views
1

我在DataMapper中使用Padrino,我試圖做一個向模型添加關聯的遷移。例如,我開始使用此: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

謝謝,我想我必須先添加一個遷移。 – 2011-01-12 19:17:08

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 

就是這樣。