将Sensor模型与其他模型连接的最佳关联方法 [英] Best association approach to connect Sensor model to others
问题描述
我正在尝试通过Flask + SQLAlchemy实现以下功能: 我有两个数据库模型,其中包含有关蜜蜂养蜂场和蜂巢的信息.我想添加功能以某种方式将它们都连接到Sensor模型.那些可以被附在蜂房之一或蜂箱之一.这就是我所拥有的.
I'm trying to implement the following with Flask+SQLAlchemy: I have two database models containing information about bee apiaries and bee hives. I would like to add feature to somehow connect both of them to Sensor model. Those can be attached to one of apiaries or one of bee hives. Here is what I have.
class Apiary(db.Model):
__tablename__ = 'apiary'
# ... fields ...
beehives = db.relationship("BeeHive", backref=db.backref('apiary', lazy='dynamic'), lazy='dynamic')
class BeeHive(db.Model)
__tablename__ = 'beehive'
# ... fields ...
apiary_id = db.Column(db.Integer(), db.ForeignKey('apiary.id'))
class SensorType(db.Model):
__tablename__ = 'sensor_type'
id = db.Column(db.Integer(), primary_key=True)
title = db.Column(db.Unicode(32), unique=True)
sensors = db.relationship('Sensor', backref=db.backref('sensor_type', lazy='dynamic'), lazy='dynamic')
class Sensor(db.Model):
__tablename__ = 'sensor'
id = db.Column(db.Integer(), primary_key=True)
serial = db.Column(UUID(), unique=True)
sensor_type_id = db.Column(db.Integer(), db.ForeignKey('sensor_type.id'))
readings = db.relationship('SensorReading', backref=db.backref('sensor', lazy='dynamic'), lazy='dynamic')
class SensorReading(db.Model):
__tablename__ = 'sensor_reading'
id = db.Column(db.BigInteger(), primary_key=True)
value = # TODO
timestamp = db.Column(db.DateTime(), default=db.func.now())
我正在Internet上冲浪,阅读SQLAlchemy文档,并发现了一些有关多态加载"的信息.我有很好的感觉,这可能就是我要寻找的东西,但是我不知道该如何实现它.我在"Django世界"中看到过类似的事情,他们称其为"GenericForeignKey".
I was surfing through the internet, reading SQLAlchemy documentation and found something about "polymorphic loading". I have good feeling that this is probably what I was searching for, but don't know how to implement it in my case. I have seen similar thing in "Django world" and they call it "GenericForeignKey".
更新:我发现了关于这种关联类型的SQLAlchemy 示例 .谁能建议我其中哪一种是最佳方法? discriminator_on_related,generic_fk,table_per_association还是table_per_related?在进一步扩展应用程序中,哪一个最令人头疼?级联删除?
UPDATE: I have found SQLAlchemy examples about this type of association. Can anyone advice me which of those would be optimum approach? discriminator_on_related, generic_fk, table_per_association or table_per_related? Which of those will be the least headache in further expanding application? Cascading delete?
推荐答案
经过两天的实验,我得出了最终结论.例子取自此源.
After two days of experiments I have came to final conclusion. Examples have been taken from this source.
-
关联鉴别器"(答案候选):
"Discriminator on association" (candidate for answer):
- (+)具有向后引用
- (?)可以有1个父对象
- (-)复杂性
通用外键":
- (+)低复杂度
- (+)具有向后引用
- (?)可以有1个父对象
- (-)程序员的代码必须照顾级联动作
每个关联的表格":
- (+)多位父母
- (+)共享表保持不变
- (?)表的数量随着相关表的数量而增加
- (-)没有向后引用
每个相关表格"(候选答案):
"table per related" (candidate for answer):
- (+)每个关联的对象都在同一张表中
- (+)可以有多个表 每个外表的
- (-)关联对象都以某种方式分开
答案:判别器"由于Sensor不具有叠加能力,因此不需要多个父母.
Answer: "Discriminator on association" as Sensor do not have ability of superposition and therefore no need for multiple parents.
这篇关于将Sensor模型与其他模型连接的最佳关联方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!