如何对一组公司之间的借贷项目进行建模

发布于 2024-11-01 07:42:42 字数 1070 浏览 1 评论 0原文

我有一组相关的公司,它们彼此共享自己拥有的物品。每个项目都有一个拥有它的公司和一个拥有它的公司。显然,拥有该物品的公司也可以拥有它。此外,公司有时会永久转让物品的所有权,而不仅仅是出借,所以我也必须考虑到这一点。

我正在尝试决定如何对物品的所有权和占有进行建模。我有一个 Company 表和一个 Item 表。

以下是我看到的选项:

  1. Inventory 表,其中包含每个 Item - Company 关系的条目。有一个指向 Companycompany 字段,并有布尔字段 is_owner 和 has_possession。
  2. 库存 表,其中包含每个商品 的条目。具有一个 owner_company 字段和一个 possessing_company 字段,每个字段都指向一个 Company
  3. 两个单独的表:ItemOwnerItemHolder**。

到目前为止,我倾向于选项三,但表格如此相似,感觉像是重复。选项二每个项目只有一行(在这方面比选项一更干净),但是在一个表上有两个字段都引用 Company 表,味道不太对(而且绘制起来很混乱)在 ER 图中!)。

数据库设计不是我的专长(我主要使用非关系数据库),所以我不知道在这种情况下最佳实践是什么。此外,我对 Python 和 Django 是全新的,所以我可能错过了一个明显的习惯用法或模式。

CompanyItem 不被所有权和占有知识污染的情况下,对此进行建模的最佳方法是什么?或者我是否因为想要让我的模型如此隔离而错过了重点?什么是 Pythonic 方式?

更新

我意识到我过于关注数据库设计。只编写好的面向对象代码并让 Django 的 ORM 来做这件事是明智的吗?

I have a group of related companies that share items they own with one-another. Each item has a company that owns it and a company that has possession of it. Obviously, the company that owns the item can also have possession of it. Also, companies sometimes permanently transfer ownership of items instead of just lending it, so I have to allow for that as well.

I'm trying to decide how to model ownership and possession of the items. I have a Company table and an Item table.

Here are the options as I see them:

  1. Inventory table with entries for each Item - Company relationship. Has a company field pointing to a Company and has Boolean fields is_owner and has_possession.
  2. Inventory table with entries for each Item. Has an owner_company field and a possessing_company field that each point to a Company.
  3. Two separate tables: ItemOwner and ItemHolder**.

So far I'm leaning towards option three, but the tables are so similar it feels like duplication. Option two would have only one row per item (cleaner than option one in this regard), but having two fields on one table that both reference the Company table doesn't smell right (and it's messy to draw in an ER diagram!).

Database design is not my specialty (I've mostly used non-relational databases), so I don't know what the best practice would be in this situation. Additionally, I'm brand new to Python and Django, so there might be an obvious idiom or pattern I'm missing out on.

What is the best way to model this without Company and Item being polluted by knowledge of ownership and possession? Or am I missing the point by wanting to keep my models so segregated? What is the Pythonic way?

Update

I've realized I'm focusing too much on database design. Would it be wise to just write good OO code and let Django's ORM do it's thing?

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

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

发布评论

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

评论(3

红ご颜醉 2024-11-08 07:42:42

您是否有理由不希望您的项目包含关系信息?感觉所有者和拥有者都是该物品的属性。

class Company(models.Model):
    pass

class Item(models.Model):
    ...
    owner = models.ForeignKey(Company, related_name='owned_items')
    holder = models.ForeignKey(Company, related_name='held_items')

一些例子:

company_a = Company.objects.get(pk=1)

company_a.owned_items.all()
company_a.held_items.all()

items_owned_and_held_by_a=Items.objects.filter(owner=company_a, holder=company_a)

items_on_loan_by_a=Items.objects.filter(owner=company_a).exclude(holder=company_a)
#or
items_on_loan_by_a=company_a.owned_items.exclude(holder=company_a)

items_a_is_borrowing=Items.objects.exclude(owner=company_a).filter(holder=company_a)
#or
items_a_is_borrowing=company_a.held_items.exclude(owner=company_a)

company_b = Company.objects.get(pk=2)

items_owned_by_a_held_by_b=Items.objects.filter(owner=company_a, holder=company_b)
#or
items_owned_by_a_held_by_b=company_a.owned_items.filter(holder=company_b)
#or
items_owned_by_a_held_by_b=company_b.held_items.filter(owner=company_a)

我认为如果您的物品仅由一家公司拥有并由一家公司持有,则不需要单独的表。如果物品可以拥有多个所有权或多个持有者,则通过库存表建立 m2m 表会更有意义。

Is there a reason why you don't want your item to contain the relationship information? It feels like the owner and possessor are attributes of the item.

class Company(models.Model):
    pass

class Item(models.Model):
    ...
    owner = models.ForeignKey(Company, related_name='owned_items')
    holder = models.ForeignKey(Company, related_name='held_items')

Some examples:

company_a = Company.objects.get(pk=1)

company_a.owned_items.all()
company_a.held_items.all()

items_owned_and_held_by_a=Items.objects.filter(owner=company_a, holder=company_a)

items_on_loan_by_a=Items.objects.filter(owner=company_a).exclude(holder=company_a)
#or
items_on_loan_by_a=company_a.owned_items.exclude(holder=company_a)

items_a_is_borrowing=Items.objects.exclude(owner=company_a).filter(holder=company_a)
#or
items_a_is_borrowing=company_a.held_items.exclude(owner=company_a)

company_b = Company.objects.get(pk=2)

items_owned_by_a_held_by_b=Items.objects.filter(owner=company_a, holder=company_b)
#or
items_owned_by_a_held_by_b=company_a.owned_items.filter(holder=company_b)
#or
items_owned_by_a_held_by_b=company_b.held_items.filter(owner=company_a)

I think if your items are only owned by a single company and held by a single company, a separate table shouldn't be needed. If the items can have multiple ownership or multiple holders, a m2m table through an inventory table would make more sense.

眸中客 2024-11-08 07:42:42
class Inventory(models.Model):
    REL = (('O','Owns'),('P','Possesses'))

    item = models.ForeignKey(Item)
    company = models.ForeignKey(Company)
    relation = models.CharField(max_length=1,choices=REL)

可能是一种实现,而不是使用布尔值。所以我会选择第一个。如果您决定使用“通过”将项目与公司相关联,这甚至可以用作中间表,如下所示:

Company:
    items = models.ManyToManyField(Item, through=Inventory)
class Inventory(models.Model):
    REL = (('O','Owns'),('P','Possesses'))

    item = models.ForeignKey(Item)
    company = models.ForeignKey(Company)
    relation = models.CharField(max_length=1,choices=REL)

Could be one implementation, instead of using booleans. So I'd go for the first. This could even serve as an intermediate table if you ever decide to use a 'through' to relate items to company like this:

Company:
    items = models.ManyToManyField(Item, through=Inventory)
白芷 2024-11-08 07:42:42

选项#1 可能是最干净的选择。一个项目只有一个所有者公司,并且仅由一个拥有公司拥有。

将两个 FK 放入 Item 中的 Company,并记住显式定义两个逆的 related_name 彼此不同。

由于您希望避免接触 Item 模型,因此可以从外部添加 FK(如在 field.contribute_to_class() 中),或者放置一个与 Item 具有一对一关系的新模型以及外键。

第二种方法更容易实现,但第一种方法一旦实现,使用起来会更自然。

Option #1 is probably the cleanest choice. An Item has only one owner company and is possessed by only one possessing company.

Put two FK to Company in Item, and remember to explicitly define the related_name of the two inverses to be different each other.

As you want to avoid touching the Item model, either add the FKs from outside, like in field.contribute_to_class(), or put a new model with a one-to-one rel to Item, plus the foreign keys.

The second method is easier to implement but the first will be more natural to use once implemented.

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