DataMapper 关联迁移

发布于 2024-10-11 10:14:41 字数 887 浏览 9 评论 0原文

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

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

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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

痴者 2024-10-18 10:14:41

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

DataMapper.auto_upgrade! will add new FK properties

诠释孤独 2024-10-18 10:14:41

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

就是这样。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文