DataMapper Association Migrations
-
09-10-2019 - |
문제
I'm using Padrino with DataMapper, and I'm trying to make a migration for adding an association to a model. For example, I begin with this:
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
And I end with the following:
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
I already have the migration for creating the three tables, but I do not for adding the associations. What would the code be for creating the migration for the associations?
해결책
DataMapper.auto_upgrade! will add new FK properties
다른 팁
auto_upgrade is nice, but won't allow incremental step back.
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
that's it.
제휴하지 않습니다 StackOverflow