如何向多对多关系添加附加信息?

时间:2022-10-04 12:44:57

I'm writing a program to manage orders and then print them.

我正在写一个程序来管理订单,然后打印它们。

An order is an object containing the ordering person, the date and the products this person orders. I'd like to add the amount of a certain product one orderer. E.g. 3 eggs, 2 breads.

订单是包含订购人员、日期和该人员订购的产品的对象。我想要添加一个特定的产品订单的数量。3个鸡蛋,2个面包。

Is there a simpler way doing this with storm (the ORM I'm using) than splitting the order into smaller pieces so that every order contains only 1 product?

对于storm(我使用的ORM),是否有一种比将订单分割成更小的部分以便每个订单只包含一个产品更简单的方法呢?

1 个解决方案

#1


3  

What's wrong with adding extra columns to the intersection table of the many-to-many relationship?

在多对多关系的交集表中添加额外的列有什么问题?

CREATE TABLE orders (
  person_id INT NOT NULL,
  product_id INT NOT NULL,
  quantity INT NOT NULL DEFAULT 1,
  PRIMARY KEY (person_id, product_id),
  FOREIGN KEY (person_id) REFERENCES persons(person_id),
  FOREIGN KEY (product_id) REFERENCES products(product_id)
);

If you use an ORM that can't access additional columns in this table while doing many-to-many queries, you should still be able to access it simply as a dependent table of either products or persons.

如果您使用的ORM在执行多对多查询时不能访问该表中的其他列,那么您仍然可以将其作为产品或人员的依赖表进行访问。

#1


3  

What's wrong with adding extra columns to the intersection table of the many-to-many relationship?

在多对多关系的交集表中添加额外的列有什么问题?

CREATE TABLE orders (
  person_id INT NOT NULL,
  product_id INT NOT NULL,
  quantity INT NOT NULL DEFAULT 1,
  PRIMARY KEY (person_id, product_id),
  FOREIGN KEY (person_id) REFERENCES persons(person_id),
  FOREIGN KEY (product_id) REFERENCES products(product_id)
);

If you use an ORM that can't access additional columns in this table while doing many-to-many queries, you should still be able to access it simply as a dependent table of either products or persons.

如果您使用的ORM在执行多对多查询时不能访问该表中的其他列,那么您仍然可以将其作为产品或人员的依赖表进行访问。