使用 DataMapper 的外键和关联

发布于 2024-12-01 20:34:01 字数 1257 浏览 2 评论 0原文

我已经非常彻底地阅读了此页面:

http://datamapper.org/docs/associations

如果答案是在那里,它根本没有以我能理解的方式表达。

我对通过 Datamapper 设置关系感到非常困惑。上面的 datamapper 网站几乎是我能找到的关于该主题的所有内容,正如我所说,它并不是特别有帮助。

例如,如果我想创建如下所示的内容:

表:users

id (primary key)
name

表:attributes

id (pk)
title

表:user_attributes

id (pk)
user_id (fk to users.id)
attribute_id (fk to attributes.id)
value

这看起来很简单,但它已经非常困难了。我尝试的所有操作都会给我带来错误,例如在 UserUserAttribute (DataMapper::UnknownRelationshipError) 中没有名为 user_attributes 或 user_attribute 的关系

有人可以告诉我这个简单映射的类定义,也许可以指出我更好的讨论数据映射器协会?以下是我尝试过的一些方法。

class User
  include DataMapper::Resource

  property :id,   Serial, :key => true
  property :name, String

  has n, :user_attributes, :through=>:attribute
end

class Attribute
  include DataMapper::Resource

  property :id,   Serial, :key => true
  property :name, String

  has n, :user_attributes, :through=>:user
end

class UserAttribute
  include DataMapper::Resource

  belongs_to :user
  belongs_to :attribute
end

I have read this page quite thoroughly:

http://datamapper.org/docs/associations

If the answer is on there, it's simply not expressed in a way I can understand.

I'm very confused about using setting up relationship via Datamapper. The datamapper site above is pretty much all I can find on the topic, and as I've said, it hasn't been particularly helpful.

For example, if I want to create something like the following:

Table: users

id (primary key)
name

Table: attributes

id (pk)
title

Table: user_attributes

id (pk)
user_id (fk to users.id)
attribute_id (fk to attributes.id)
value

This seems simple enough, but it has been prohibitively difficult. Everything I try gives me errors like No relationships named user_attributes or user_attribute in UserUserAttribute (DataMapper::UnknownRelationshipError)

Can someone please tell me the class definitions for this simple mapping, and perhaps point me to a better discussion of DataMapper associations? Below is some of what I've tried.

class User
  include DataMapper::Resource

  property :id,   Serial, :key => true
  property :name, String

  has n, :user_attributes, :through=>:attribute
end

class Attribute
  include DataMapper::Resource

  property :id,   Serial, :key => true
  property :name, String

  has n, :user_attributes, :through=>:user
end

class UserAttribute
  include DataMapper::Resource

  belongs_to :user
  belongs_to :attribute
end

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

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

发布评论

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

评论(2

清风不识月 2024-12-08 20:34:01

我认为您会看到类似 UserUserAttribute 的内容,因为 DataMapper 正在尝试自动生成匿名连接类。

这是一篇文章,介绍如何命名多个DataMapper 中的对多关系

您可能会更改示例,如下所示:

  • User ->用户
  • 项目->属性
  • 协作->用户属性

I think you're seeing things like UserUserAttribute because DataMapper is trying to auto-generate an anonymous join class.

Here's an article that describes how to make named many-to-many relationships in DataMapper

You would probably change the example something like this:

  • User -> User
  • Project -> Attribute
  • Collaboration -> UserAttribute
绝情姑娘 2024-12-08 20:34:01

问题是您的类是 UserAttribute,并且您试图用 user_attributes 命名关系。去掉下划线,你的问题就会消失(或者在类名中添加下划线)

The issue is that your class is UserAttribute, and you're trying to name the relationships with user_attributes. Get rid of the underscore, and your issue will disappear (or add an underscore to the class name)

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