1

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?

Ethan Turkeltaub
  • 2,931
  • 8
  • 30
  • 45

2 Answers2

2

DataMapper.auto_upgrade! will add new FK properties

solnic
  • 5,733
  • 2
  • 23
  • 19
1

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.

phil pirozhkov
  • 4,740
  • 2
  • 33
  • 40