Search code examples
migrationdatamapperpadrino

DataMapper Association Migrations


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?


Solution

  • DataMapper.auto_upgrade! will add new FK properties