Sqlalchemy多个类链接到同一儿童课

发布于 2025-02-12 11:34:46 字数 2301 浏览 0 评论 0原文

我有多个产品,每个产品都有一个定价对象的列表来记录这些对象的历史价格。我似乎无法缠绕我的脑海,这将是什么关系。

class Apple:
    __tablename__ = 'Apple'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    prices= relationship("Pricing")

class Banana:
    __tablename__ = 'Banana'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    prices= relationship("Pricing")

class Pear:
    __tablename__ = 'Pear'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    prices= relationship("Pricing")

因此,基本上,每个产品(或水果)都会有定价对象的列表。定价列的列为:PricingDatePriceValue

那么,如何编写定价类的代码?本质上应该看起来像这样的桌子:

IDPricingDatePriceValue
12019-06-012.00
22019-05-286.08 6.08
32022-04-0710.42
42018-12-12 3.45 5 2014-09-09-089.20
62022--089.20
62022--08 08-3125.34

定价表不需要跟踪每一行的水果,因为我只能访问首先,水果,然后检索定价记录。本质上,字典应该看起来像这样:

apple_records = [
    {
        'id': 1,
        'name': 'first_apple',
        'prices': [
            {
                'id': 1,
                'PricingDate': '2019-06-01',
                'PriceValue': 2.00
            }
        ]
    },
    {
        'id': 2,
        'name': 'second_apple',
        'prices': [
            {
                'id': 4,
                'PricingDate': '2018-12-12',
                'PriceValue': 3.45
            }
        ]
    }
]

本质上,我不会直接从pricingtable中读取。我所需要的是,所有水果都会有定价对象的列表,因此Crud变得容易,我希望Sqlalchemy为我处理这一点。又称删除/更新的级联等。

从我的脑海中流行的想法/选项:

  1. 从外观上看,似乎我只需要一对一的亲子关系。
  2. 但事实并非如此,因为我现在有许多不同类型的父母(不同类型的水果)
  3. 我必须创建多个关联表吗?这意味着如果我有50种不同的水果,我需要创建50个关联表?
  4. 同样,水果的数量未知,我将动态创建这些类。
  5. 我是否必须在定价表中创建多个foriegn键,每种类型的水果的一列?
  6. c#角度来看,这是类似类可以实现ipricing接口,并且多个类可以继承/实现它,只要它们具有属性定位列表

I have multiple products that each, has a list of Pricing objects to record the historical price of those objects. I can't seemed to wrap around my head what will the relation be.

class Apple:
    __tablename__ = 'Apple'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    prices= relationship("Pricing")

class Banana:
    __tablename__ = 'Banana'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    prices= relationship("Pricing")

class Pear:
    __tablename__ = 'Pear'
    id = Column(Integer, primary_key=True)
    name = Column(String)
    prices= relationship("Pricing")

so basically each product (or fruit) will have a list of Pricing objects. The columns of PricingTable are: PricingDate and PriceValue.

So how would I write the code for Pricing class? Essentially the table ought to look like this:

idPricingDatePriceValue
12019-06-012.00
22019-05-286.08
32022-04-0710.42
42018-12-123.45
52014-09-089.20
62022-08-3125.34

The Pricing table doesn't need to keep track of what fruit is for each row, as I would only access the fruits first, then retrieve it's Pricing records. In essence, the dictionary ought to look something like this:

apple_records = [
    {
        'id': 1,
        'name': 'first_apple',
        'prices': [
            {
                'id': 1,
                'PricingDate': '2019-06-01',
                'PriceValue': 2.00
            }
        ]
    },
    {
        'id': 2,
        'name': 'second_apple',
        'prices': [
            {
                'id': 4,
                'PricingDate': '2018-12-12',
                'PriceValue': 3.45
            }
        ]
    }
]

In essence I wouldn't really read from the PricingTable directly. All I need is the all fruits will have a list of Pricing objects, so that CRUD comes easy and I would want SQLAlchemy to handle that for me; aka delete/update cascade etc.

Thoughts/Options that went through my head:

  1. From the looks of it, it seems like I just need a one-to-many parent-child relationship.
  2. But that's not the case, as I now have many, distinct types of parents (different types of fruits)
  3. Do I have to create multiple association tables? Would that mean if I have 50 distinct fruits, I would need to create 50 association tables?
  4. Also the number of fruits are unknown, I am going to create those classes dynamically.
  5. Do I have to create multiple foriegn keys in Pricing table, one column for each type of fruit that exists?
  6. From c# perspective, this is something like a class can just implement an IPricing interface, and multiple class can inherit/implement it, as long as they have the attributes PricingList

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

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

发布评论

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

评论(1

倾城°AllureLove 2025-02-19 11:34:47

从外观上看,我似乎只需要一对一的亲子关系。
但事实并非如此,因为我现在有许多不同类型的父母(不同类型的水果)
我必须创建多个关联表吗?这意味着如果我有50种不同的水果,我需要创建50个关联表?

您正在寻找多态性 sqlalchemy中。这是一个复杂的话题,在这个问题的背景下不能广泛回答。有多种方法可以解决此问题,但是由于问题没有讨论更大的一般问题,因此无法指出最好的方法。

也未知水果的数量,我将动态创建这些类别。

这听起来好像您做错了什么。尽管可能需要动态生成Python类,但您的代码可能会出现更大的架构问题。因为这个问题没有足够的上下文,所以无法指出任何更好的解决方案。

我是否必须在定价表中创建多个foriegn键,每种类型的水果的一列?

参见上面的多态性。

从c#角度来看,这是类似类可以实现iPricing接口,并且多个类可以继承/实现它,只要它们具有属性pricinglist

从c#角度来看,这是类似的类似,只要pricinglist python具有

From the looks of it, it seems like I just need a one-to-many parent-child relationship.
But that's not the case, as I now have many, distinct types of parents (different types of fruits)
Do I have to create multiple association tables? Would that mean if I have 50 distinct fruits, I would need to create 50 association tables?

You are looking for polymorphism in SQLAlchemy. It's a complex topic and cannot be broadly answered in the context of this question. There are multiple ways to solve this, but because the question does not discuss about the larger general problem, it is not possible to point out what would the best way.

Also the number of fruits are unknown, I am going to create those classes dynamically.

This sounds like you are doing something wrong. Although it is possible if you need to dynamically generate Python classes you have likely a larger architectural problem with your code. Because the question does not have enough context, it is not possible to point out any better solutions.

Do I have to create multiple foriegn keys in Pricing table, one column for each type of fruit that exists?

See polymorphism above.

From c# perspective, this is something like a class can just implement an IPricing interface, and multiple class can inherit/implement it, as long as they have the attributes PricingList

Python has a concept of abstract base classes that can act as an interface, as Python supports multiple inheritance.

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