1

我正在将 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

我已经有了创建三个表的迁移,但我没有添加关联。为关联创建迁移的代码是什么?

4

2 回答 2

2

DataMapper.auto_upgrade!将添加新的 FK 属性

于 2011-01-10T09:08:10.530 回答
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

就是这样。

于 2011-07-25T19:17:13.640 回答