我正在使用Padrino和DataMapper,我正在尝试进行迁移,以便为模型添加关联。例如,我从这开始:
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
我已经有了创建三个表的迁移,但我没有添加关联。为代码创建迁移的代码是什么?
答案 0 :(得分:2)
DataMapper.auto_upgrade!将添加新的FK属性
答案 1 :(得分: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
就是这样。