知识卡片
一对多关系由外键连接
一个订单可包含多个条目,因此可把订单和条目分成两张表,让每个条目以 order_id 外键指向所属订单。对象层可提供 order.items 这样的关系访问,但真正的一对多约束落在数据模型与外键上;设计时还须检查主键、外键类型是否一致。
这张卡片看不懂?
这张卡片的反馈已满 10 条,暂时无法继续提交。
已有反馈 · 0 / 10 条
还没有人提交反馈。
原书出处
参考来源
- 位置:《Microservice APIs》第 7 章《Service implementation patterns for microservices》(源文件:_epub-src/OEBPS/Text/07.htm)
- 结论依据:原文说明订单与条目是一对多,条目的 `order_id` 外键连接所属订单,并演示 ORM 的 `relationship()` 属性。书中代码的具体类型可能有误,留待 S3 核验。
- 原始内容:There’s a one-to-many relationship between orders and items, and therefore we’ll implement a model for items and link them with a foreign key relationship. ... Each item also maps to the order it belongs to through the order_id property, which is defined as a foreign key column.